简体   繁体   中英

What does ^ operator do?

I thought that ^ did that. I expected:

10^0=1
10^1=10
10^2=100

What I'm getting

10^0=10
10^1=11
10^2=8

the actual code is

int value = 10 ^ exp;

replacing exp for 0, 1, and 2 What does the ^ operator do?

Math.Pow(x, y) to get x raised to the power of y. You were doing an XOR. C# operators

You want to do:

Math.Pow(10, exp);

This actually produces a double though so you'll need to cast it down if you really want an int .

In c#, ^ is the logical XOR operator .

As others have said, you need you use Math.pow(x, y) to do x^y in C#. The ^ operator is actually a logical XOR operator on the bits of the two numbers. More information on the logical XOR can be found here: http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx

There is no operator for this. Use Math.Pow .

There is a full list of all the C# operators with linked documentation on each one.

Math.Power(10,exp) works like charm...

In C#, the ^ operator is the logical XOR operator.

http://msdn.microsoft.com/en-us/library/6a71f45d.aspx

^ is the XOR operator.

http://en.wikipedia.org/wiki/Xor

If you want to raise a number to a power, use

Math.Pow(num, exp);

The thing is that ^ is equivalent for XOR , that's why 10 ^ 0 = 10 because

1010 XOR    1010 XOR
0000 =      0010 =
1010        1000

You need to use Math.Pow method.

PS = 10^2 actually returns 8...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM