-12

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?

takendarkk
  • 3,347
  • 8
  • 25
  • 37

6 Answers6

9

It is the C/C++/C#/Java/Javascript/Perl bitwise xor assignment operator.

ldav1s
  • 15,885
  • 2
  • 53
  • 56
5

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.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
2

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.

Scott Hunter
  • 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
2

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

Doon
  • 19,719
  • 3
  • 40
  • 44
0

In jQuery it means "starting with this"

For example id^='my_'

means "id starts with 'my_'"

dkellner
  • 8,726
  • 2
  • 49
  • 47
0

In the php manual it says that ^ is the bitwise XOR operator. Here's the link

Dries
  • 995
  • 2
  • 16
  • 45