0

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

Eric Leschinski
  • 135,913
  • 89
  • 401
  • 325
user1276509
  • 59
  • 1
  • 4

2 Answers2

19

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,154
  • 3
  • 33
  • 48
2

See Bitwise and Bit Shift Operators.

ArtemStorozhuk
  • 8,660
  • 4
  • 32
  • 52