What does the ^
(caret) mean in Java syntax? Why does 6^3
return 5
?
Asked
Active
Viewed 1.1k times
1

Eric Leschinski
- 146,994
- 96
- 417
- 335

user1276509
- 69
- 1
- 1
- 4
-
2It's the lesser-used XOR operation. 1^0 = 1 See: http://stackoverflow.com/questions/460542/operator-in-java – Caffeinated Apr 18 '12 at 20:26
-
2Adel, you should create an answer. – Hiro2k Apr 18 '12 at 20:26
-
2@Hiro2k - I did! But some gremlin in the SO-code automatically made it a comment. I am bitter now : – Caffeinated Apr 18 '12 at 20:27
-
The same thing it means in every programming language? – Brian Roach Apr 18 '12 at 20:29
-
@BrianRoach - Wait.. hmm, in python RegEXP it's the string start? – Caffeinated Apr 18 '12 at 20:30
-
1@Brain: It's exponentiation in Visual Basic... And superscript in LaTeX... And beginning of line/string (depending on mode) in regular expressions... And "start going upwards" in befunge... And I think it's even "logical and" in APL. – cha0site Apr 18 '12 at 20:31
-
1@Adel - A regex is not a programming language. It has the same meaning there in all languages as well. In this context, however, he clearly doesn't mean in a regex as shown in his question. – Brian Roach Apr 18 '12 at 20:32
-
@BrianRoach - Gotcha, good point. Thank You So Much – Caffeinated Apr 18 '12 at 20:32
-
@cha0site - Yet another reason not to consider VB a programming language :-D – Brian Roach Apr 18 '12 at 20:35
2 Answers
20
It's the bitwise XOR operator. XOR is exclusive or.
6 in binary (lets assume 4 bits) is 0110
, 3 in binary is 0011
.
So, we get:
0110
0011 XOR
----
0101
And 0101
is 5.

cha0site
- 10,517
- 3
- 33
- 51
-
Its also the *boolean* XOR operator, also that is probably used less often. – Jörn Horstmann Apr 18 '12 at 22:14