What the Bitwise XOR Operator Does
Bitwise XOR (exclusive OR, written ^ in most languages) outputs 1 when the two input bits are different and 0 when they are the same.
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Read the table one row at a time and a second interpretation appears: when B is 0, the output equals A; when B is 1, the output is A flipped. XOR is therefore a controlled inverter. The second operand chooses which bits of the first operand get toggled.