# "^" operator What does it mean?

**URL:** https://discourse.processing.org/t/operator-what-does-it-mean/4290
**Category:** Beginners
**Created:** [October 9, 2018, 6:37pm UTC](https://discourse.processing.org/t/operator-what-does-it-mean/4290 "2018-10-09T18:37:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Aquakitty](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aquakitty/32/1906_2.png) [@Aquakitty](https://discourse.processing.org/u/Aquakitty)
#### Post date: [October 9, 2018, 6:37pm UTC](https://discourse.processing.org/t/operator-what-does-it-mean/4290/1 "2018-10-09T18:37:15Z")

</div>

Hi,  
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.

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [October 9, 2018, 7:07pm UTC](https://discourse.processing.org/t/operator-what-does-it-mean/4290/2 "2018-10-09T19:07:25Z")

</div>

`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.”

Whew.

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

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [October 9, 2018, 8:10pm UTC](https://discourse.processing.org/t/operator-what-does-it-mean/4290/3 "2018-10-09T20:10:22Z")

</div>

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

[Processing.org/reference/boolean.html](http://Processing.org/reference/boolean.html)

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

1. [Processing.org/reference/bitwiseAND.html](http://Processing.org/reference/bitwiseAND.html) (`&`)
2. [Processing.org/reference/bitwiseOR.html](http://Processing.org/reference/bitwiseOR.html) (`|`)
3. [Processing.org/reference/logicalAND.html](http://Processing.org/reference/logicalAND.html) (`&&`)
4. [Processing.org/reference/logicalOR.html](http://Processing.org/reference/logicalOR.html) (`||`)

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

> **[Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)**
>
> Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true. I...

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

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

> **[Exclusive or](https://en.wikipedia.org/wiki/Exclusive_or)**
>
> Exclusive or or exclusive disjunction is a logical operation that outputs true only when inputs differ (one is true, the other is false).
> It is symbolized by the prefix operator J and by the infix operators XOR (/ˌɛks ˈɔːr/), EOR, EXOR, ⊻, ⩒, ⩛, ⊕, ↮, and ≢. The negation of XOR is logical biconditional, which outputs true only when both inputs are the same.
> It gains the name "exclusive or" because the meaning of "or" is ambiguous when both operands are true; the exclusive or operator exclude...

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.

---

<div class="post-metadata">

### Author: ![Aquakitty](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aquakitty/32/1906_2.png) [@Aquakitty](https://discourse.processing.org/u/Aquakitty)
#### Post date: [October 9, 2018, 10:44pm UTC](https://discourse.processing.org/t/operator-what-does-it-mean/4290/4 "2018-10-09T22:44:42Z")

</div>

Awesome, thank you both.
