I just came across an answer here on SO where a code example (in what I thought was java) used an operator I have never seen before: ^=
. I searched google and SO and haven't been able to find it anywhere else and when I tried to test it out using java, eclipse went crazy. Maybe it was just a typo in the answer, I'm not sure. What is this operator? Does it have a name? What language is it from or used in?

- 3,347
- 8
- 25
- 37
6 Answers
This is the C/C++/C#/Java/Javascript/Perl/PHP/Pike bitwise XOR assignment operator.
An XOR (exclusive or) conditional statement evaluates to true if and only if one of the two operands involved is true.
Example:
0 ^ 0 = false
1 ^ 0 = true
0 ^ 1 = true
1 ^ 1 = false //Regular OR would evaluate this as true
In the same way that you can use +=
-=
*=
/=
etc... this operator can be combined with an equals sign to perform assignment upon completion.
x += 1; //Same as x = x + 1;
t ^= f; //Same as t = t ^ f;
boolean a = false;
boolean b = true;
a ^= b; //a now evaluates to true;
See Java Operators.

- 3,347
- 8
- 25
- 37
It would depend on the language, but ^ is typically an exclusive-or (example languages: java, c-family); a ^= b is shorthand for a = a^b.

- 48,888
- 12
- 60
- 101
-
@csmckelvey Look att http://en.wikipedia.org/wiki/Xor The truth-table: 0 ^ 0 = 0, 0 ^ 1 = 1, 1 ^ 0 = 1, 1 ^ 1 = 0 – some Dec 27 '13 at 01:28
take a look here
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html and http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
but it is a Bitwise Exclusive or with assignment

- 19,719
- 3
- 40
- 44
In jQuery it means "starting with this"
For example id^='my_'
means "id starts with 'my_'"

- 8,726
- 2
- 49
- 47
-
1Why write an answer about jQuery if the question is about Java? (see tags) – Aurelio De Rosa Dec 27 '13 at 01:43
-
@AurelioDeRosa At the time when this answer was written, there where no tag about java, only `operators`. – some Dec 27 '13 at 02:44
-
1