0

Possible Duplicate:
^ operator in java

I was assuming that c ^ d is a calculation like 'the power of', so c = 5, d = 2, result is 25. I think I'm wrong, though.

Can you explain what (c ^ d) does in java, for example in

result = result + (char)(c ^ d)
Community
  • 1
  • 1
Kees Koenen
  • 772
  • 2
  • 11
  • 27
  • 2
    ^ is a bitwise XOR. http://stackoverflow.com/questions/460542/operator-in-java –  Jan 25 '12 at 20:33

4 Answers4

3

The ^ operator performs a Bitwise Exclusive OR.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

For raising a number to a power, you would use the Math.pow function.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
3

^ is the bitwise xor meaning that 0b0101^0b0010 (5^2) is 0b0111 and 0b0101^0b0111 is 0b0010

look at the truth table of xor (the result is 1 if the input are different

a b | a^b
---------
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0

the bitwise operators take each bit of the terms and apply the operator to each bit

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
1

That one is the bitwise XOR operator.

Tudor
  • 61,523
  • 12
  • 102
  • 142
1

^ is a bitwise XOR.

Community
  • 1
  • 1