Beetwise

Bitwise XOR

The XOR operation compares the two values bit by bit.

A bit in the result has the value 1 if the two inputs differ at that position.

XOR is the operation that flips one or more bits.

Try bitwise XOR in the bit editor

The result of 12 XOR 10. Edit the expression or select a bit to flip it.

7
6
5
4
3
2
1
0
6
value · 8 bits · unsigned
hex
0x06

Bitwise XOR step by step

Calculate 12 XOR 10:

  1. Write the first value: 1100.
  2. Write the second value: 1010.
  3. Set each position where the two values differ.
  4. The result is 0110, which is 6.

The truth table of XOR

The truth table of XOR
ABA ^ B
000
011
101
110

Where bitwise XOR is used

A toggle of a status LED is an XOR with the mask of that bit.

A simple checksum uses XOR over all the bytes of a message.

A second XOR with the same mask restores the original value.

Bitwise XOR: points to note

  • XOR with the same value twice gives the original value. This is the reason XOR suits a toggle.
  • XOR with 0 does not change the value. XOR with a mask of all ones inverts the value.
  • A XOR checksum does not detect a swap of two bytes.

The full bit editor

The full editor holds many values, evaluates expressions across them, and reads live values from a serial port.

Open this value in the full editor

Questions about bitwise XOR

What is 12 XOR 10?
The result is 6. The bits differ at positions 1 and 2, which gives 0110.
How do I flip one bit?
Apply XOR between the value and a mask that has only that bit set. In C this is value ^= mask.
Why is XOR used for a checksum?
XOR is fast and needs no extra memory. It detects a change of a single bit.