Compute bitwise XOR of two binary numbers instantly
Try an example:
Tip: XOR (exclusive OR) returns 1 when the two input bits are different, and 0 when they are the same. The result is also shown in hexadecimal.
XOR stands for "exclusive OR." It's a logical operation that outputs true (1) only when the two inputs are different. If both inputs are the same — both 0 or both 1 — the output is false (0). The easiest way to remember XOR is: "one or the other but not both."
Mathematically, XOR is written as A ⊕ B. The truth table has only four rows, and once you memorize it, you can XOR any two binary numbers in your head for short sequences:
XOR shows up everywhere in computing because of one key property: it's reversible. If you XOR a value twice with the same key, you get back the original. This makes XOR the foundation of several important techniques:
XOR is the building block of many encryption algorithms. A simple XOR cipher works by XORing plaintext bytes with a key. To decrypt, you XOR the ciphertext with the same key. While a single XOR cipher is easy to break with frequency analysis, modern ciphers like AES use XOR internally as one of their core operations alongside substitution and permutation.
XOR is used to generate parity bits in error detection. XORing all the bits of a data word tells you whether the number of 1s is odd or even. RAID arrays use XOR parity to reconstruct data when a drive fails — they XOR all the remaining drives to recover the missing one.
In computer graphics, XOR is often used for drawing cursors and selection boxes. XORing pixels with the background is fast and reversible — drawing the same shape again in the same location erases it and restores the original pixels underneath.
There's a classic trick that uses XOR to swap two variables without needing a third temporary variable. In C: a ^= b; b ^= a; a ^= b;. After those three XOR operations, the values of a and b are swapped. It works because of XOR's reversibility property.
| Input A | Input B | A ⊕ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
This is the complete XOR truth table. Memorize these four rows and you can XOR any two binary values.
Most programming languages use the ^ operator for bitwise XOR. Here's how it looks in common languages:
int result = a ^ b;int result = a ^ b;result = a ^ blet result = a ^ b;int result = a ^ b;result := a ^ blet result = a ^ b;result = a ^ bIn every case, ^ performs a bitwise XOR — it compares the two values bit by bit and produces a result where each bit follows the XOR truth table.