1

What does the ^ (caret) mean in Java syntax? Why does 6^3 return 5?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user1276509
  • 69
  • 1
  • 1
  • 4

2 Answers2

20

It's the bitwise XOR operator. XOR is exclusive or.

6 in binary (lets assume 4 bits) is 0110, 3 in binary is 0011.

So, we get:

0110
0011 XOR
----
0101

And 0101 is 5.

cha0site
  • 10,517
  • 3
  • 33
  • 51
2

See Bitwise and Bit Shift Operators.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53