73

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

Trevor Hickey
  • 34,154
  • 27
  • 144
  • 256
Warrior
  • 38,748
  • 44
  • 137
  • 212

5 Answers5

99

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

false ^ false == false
true ^ false == true
false ^ true == true
true ^ true == false 
eckes
  • 9,711
  • 1
  • 55
  • 69
Serafina Brocious
  • 30,005
  • 11
  • 87
  • 112
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,289
  • 2
  • 37
  • 59
6

It's bitwise XOR.

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

empi
  • 15,465
  • 8
  • 60
  • 76
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
  • 28,481
  • 11
  • 62
  • 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
  • 15,693
  • 21
  • 74
  • 120