Can anyone explain the use of ^ operator in java with some examples?
Asked
Active
Viewed 5.9k times
5 Answers
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
-
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
-
BTW: 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,289
- 2
- 37
- 59
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.
DonX
- 15,693
- 21
- 74
- 120