0

I just saw it in a code, so I tried this :

int i = 30;
System.out.println(i^3);

Result is :

29

What is this ? Thanks.

Rob
  • 15,732
  • 22
  • 69
  • 107

4 Answers4

5

It's the Xor - exclusive bitwise or.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
4

^ its an exclusove OR operator (XOR). Its actually bitwise sum % 2.

11110 (30)
00011 (3)
----------
11101 (29)
kaysush
  • 4,797
  • 3
  • 27
  • 47
  • Thanks a lot for this explanation, I mainly wanted to know why this returns 29. – Rob Mar 05 '13 at 15:59
3

^ in Java is the XOR operator

XOR stands for a bitwise Exclusive OR. IE:

0 XOR 0 = 0

0 XOR 1 = 1

If the bit's are different (exclusive) then the output is a 1, otherwise a 0.

So following your example:

11110 XOR 00011 = 11101 = 29

christopher
  • 26,815
  • 5
  • 55
  • 89
3

It is called Bitwise exclusive OR Operator

PermGenError
  • 45,977
  • 8
  • 87
  • 106