73

Can anyone explain the use of ^ operator in java with some examples?

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Warrior
  • 39,156
  • 44
  • 139
  • 214

5 Answers5

100

This is the same as ^ in most languages, just an XOR.

false ^ false == false
true ^ false == true
false ^ true == true
true ^ true == false 
eckes
  • 10,103
  • 1
  • 59
  • 71
Serafina Brocious
  • 30,433
  • 12
  • 89
  • 114
9

Some of the other answers only say it is a bitwise XOR, but note that it can also be a logical XOR if the operands are of boolean type, according to this source.

Museful
  • 6,711
  • 5
  • 42
  • 68
6

It's bitwise XOR.

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

empi
  • 15,755
  • 8
  • 62
  • 78
4

That's the bitwise exclusive OR operation. Check out the Bitwise and Bit Shift Operators section of the Java tutorials for more information.

Zach Scrivena
  • 29,073
  • 11
  • 63
  • 73
4

In java ^ operator used for bitwise XOR operation.

Follow this link to see the operator precedence also.

http://www.uni-bonn.de/~manfear/javaoperators.php

DonX
  • 16,093
  • 21
  • 75
  • 120