Can anyone explain the use of ^ operator in java with some examples?
Asked
Active
Viewed 6.2k times
5 Answers
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
-
14Well, not _any_ language - VB uses ^ for exponentiation. – gkrogers Jan 20 '09 at 09:15
-
26Yes but VB always uses different stuff anyway... ;) – Pierre-Adrien Oct 11 '09 at 23:38
-
5Lua also uses ^ for exponentiation. – Ziggy Nov 21 '14 at 21:40
-
1BTW: It could be repleced by simple `a != b` – Grigory Kislin Jun 28 '19 at 20:02
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
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.

DonX
- 16,093
- 21
- 75
- 120