"^" operator What does it mean?

I’m pretty new to processing and I came across something like this:

buttonClicked ^= true;

inside a mousePressed() function. What does this “^” do/mean? I did try searching and Googling but I think this character is omitted from all searches.

Thank you.

x ^= y; is shorthand for x = x ^ y;

The ^ symbol is an XOR operation. So x = x ^ y means “If x is true and y is false or x is false and y is true, set x to true. But if x and y are both false, or both true, set x to false.”


Of course, in this specific case, y is always true. So x ^= true; means x = x ^ true;, which is the same as x = !x;


In Java, the binary bitwise operators &, | and ^, which normally set & reset bits of integral numbers, are overloaded to deal w/ Boolean values (true & false) as well when both operands are of datatype boolean:


Binary bitwise operators & and | correspond respectively to the binary logical operators && and ||:

  1. Processing.org/reference/bitwiseAND.html (&)
  2. Processing.org/reference/bitwiseOR.html (|)
  3. Processing.org/reference/logicalAND.html (&&)
  4. Processing.org/reference/logicalOR.html (||)

However, the former 1s don’t short-circuit like the latter 1s:

And as you can notice, there’s no corresponding logical operator for ^.

The operator ^ corresponds to XOR (eXclusive OR) logical operation:

Which evaluates to false or 0 if both inputs are the same. true or 1 otherwise.

The statement buttonClicked ^= true; in effect toggles the boolean value of variable buttonClicked.

When buttonClicked is currently false: the operation false ^ true evaluates to true, b/c both operands are different.

When buttonClicked is currently true: the operation true ^ true evaluates to false, b/c both operands are equal.


Awesome, thank you both.