TL;DR
Exclusive or, written XOR, takes two bits and outputs 1 when they differ and 0 when they match. Applied to whole numbers, it works bit by bit, so a single operation mixes or compares every bit at once. It is cheap to build in hardware and it can be undone by applying it again, which makes it useful for parity checks and for simple encryption.
Builds on: Binary Numbers (Bits and Bytes), Boolean Algebra · Leads to: Symmetric Encryption, and the adders and parity circuits built from logic gates
Where it's used
Disk arrays with parity. Some RAID systems store, next to the data on several disks, a parity block made by XORing the data blocks together. If one disk fails, XORing the parity with the surviving disks gives back the missing data. The disks in such an array can be rebuilt without storing a full second copy.
Adders inside processors. The sum bit of two one-bit numbers is the XOR of the two bits. Every arithmetic unit that adds binary numbers contains many XOR gates, because the sum digit at each position is an XOR of the two input digits.
Stream ciphers and one-time pads. To encrypt a message, the sender XORs each bit of the message with a bit of a secret key stream. The receiver XORs the same key stream again and gets the message back. Modern ciphers use XOR at many steps, although the security comes from the key and the design, not from XOR alone. The simplest version of this scheme is the one-time pad.
How it works
Take two bits, and . The operation is written (read "a XOR b"), and the rule is short: the output is 1 when exactly one input is 1.
- Choose the inputs. Let and . Check: each input is a single bit, 0 or 1.
- Compare the bits. The bits differ, since one is 1 and the other is 0. Check: .
- Output 1. Because the inputs differ, the output is 1. So . Check: the full set of four input pairs is listed in the truth table in The math section.
- Apply it to whole numbers. Take the 4-bit binary numbers and . Compare bit by bit, left to right: , , , . The result is , which is 11. Check: in each column the two bits differ or match, and the output follows the rule.
- Undo it. XOR the result with again: , which is 13. Check: the original number came back, with no information lost.
flowchart LR
A["Two bits a and b"] --> B{"Do they differ?"}
B -- yes --> C["Output 1"]
B -- no --> D["Output 0"]
C --> E["Apply the same XOR again to undo it"]
D --> E
Step 5 is the reason XOR is so useful. Applying the same key twice cancels the key out, and that is the basis of the encryption use in the next section.
The math
Write the rule with the operations from Boolean algebra, where is AND, is OR and is NOT. The rewriting rules for NOT over AND and OR are the De Morgan laws, which you may use to check the equivalent forms:
Read it as: the output is 1 when is 1 and is 0, or when is 0 and is 1.
The full truth table has four rows:
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Step 3 in symbols: the rule for one pair
For , , the first term is , and the second term is . The OR of the two is .
Step 4 in symbols: bitwise on whole numbers
For -bit numbers and , is the number whose -th bit is . The operation is applied independently to each position, with no carries between positions. That is what separates it from ordinary addition.
Step 5 in symbols: XOR undoes itself
Two properties give the undo rule:
- , because two equal bits match.
- , because 0 changes nothing.
XOR can be regrouped freely: it is associative, meaning . It is also commutative, meaning the order of the two inputs does not matter, so . Together these give .
So for any key , .
Parity
For more than two bits, is 1 exactly when an odd number of the inputs are 1. This is the parity rule behind parity bits, which add one check bit so that the total number of 1s is always even. A mismatch then signals that a bit has flipped.
Counting differences
Given two equal-length bit strings, has a 1 in exactly the positions where they differ. The number of 1s in that result, called the Hamming distance, counts how many bits differ between the two strings.
Worked examples
Find and using the rule that the output is 1 when the inputs differ.
- : the inputs are both 1, so they match. The output is 0.
- Check against the table: row , gives 0.
- : the inputs are 0 and 1, which differ. The output is 1.
- Check against the table: row , gives 1.
- Interpretation: XOR is 1 for mismatched inputs and 0 for matched ones. It is not the same as OR, which is also 1 when both inputs are 1.
Compute bit by bit, then check the result by XORing it with .
- Column 1 (leftmost): .
- Column 2: .
- Column 3: .
- Column 4: .
- Result: . In decimal, , and .
- Check: . Column by column, , , , , giving , which is the original 10.
- Interpretation: the XOR of two numbers and one of them gives the other. This is the undo property in action.
A three-disk array stores data on disk A and disk B, and parity on disk P. Disk A holds and disk B holds . Find the parity, then recover disk B after it fails.
- The parity is the XOR of the two data blocks: .
- Column by column: , , , . So .
- Check: should give , which is disk B. Column by column: , , , , giving .
- Disk B fails. Recover it from the parity and disk A: .
- The result is , the original contents of disk B.
- Interpretation: one extra disk of parity protects any single disk. The rebuild needs only XOR operations, which are fast, and no second copy of the data.
Try it: disk A holds and the parity block is . Disk B has failed. What was on disk B?
Answer: . Column by column: , , , . So disk B held .
Common mistakes and tradeoffs
It is tempting to think XOR is just OR. In fact they differ on one row: OR gives 1 for , while XOR gives 0 for , because the inputs match. This is why it is called "exclusive".
It is tempting to think XOR encrypts a message securely by itself. In fact security depends on the key. If the same key is reused for two messages, then , and an attacker learns the relation between the two messages without the key. The one-time pad avoids this by using a fresh random key of the same length as the message, used only once.
It is tempting to think XOR of whole numbers behaves like addition. In fact XOR has no carries, so with nothing carried to the next column. Addition carries and XOR does not.
When to use it: to combine or compare bits cheaply, to build parity checks, to undo a masking step, and to mix a key into data in a reversible way.
When not to use it: as the only protection for secret data, or when the key is shorter than the message or reused. Use a vetted cipher instead.
Alternatives: AND and OR are used for masking, where only some bits are kept or set. A Checksum also detects errors, but an XOR parity block can additionally rebuild a lost piece. Addition modulo is the same as XOR on single bits, which ties this operation to Modular Arithmetic. For stronger secrecy, the one-time pad and the ciphers described in Symmetric Encryption build on XOR with carefully chosen keys.
History
George Boole published the algebra of logic in 1847, showing that reasoning about true and false could be written as arithmetic on two values. Boolean algebra became the mathematics of switches and circuits when Claude Shannon's 1937 master's thesis showed how to design relay circuits with it.
In the late 1910s, Gilbert Vernam worked on a teleprinter cipher that combined each message character with a key tape, a bitwise operation equivalent to XOR. When the key tape is used only once, the scheme becomes the one-time pad. In 1949, Claude Shannon showed that a one-time pad with a truly random key that is used once gives perfect secrecy.
XOR became a standard gate in circuits partly because the sum bit of binary addition is exactly XOR. It has stayed one because it is simple to build and reverses itself.
Recap
- XOR outputs 1 when two bits differ and 0 when they match.
- Applied to whole numbers, it works column by column with no carries.
- Applying the same XOR key twice gives back the original, because and .
- Parity and the sum bit of addition are XOR operations, which is why XOR gates are everywhere in processors and storage systems.
- Read next: Symmetric Encryption, which uses a shared secret key, and One-Time Pad, where the key is as long as the message and used once.
Check yourself
QuestionWhat is the output of , and why?Show answer
The output is 1. The inputs differ, one 1 and one 0, and XOR outputs 1 exactly when the inputs differ.
QuestionCompute bit by bit.Show answer
Column by column: , , , . The result is , which is 8.
QuestionWhy does applying the same XOR key twice return the original message?Show answer
Because and . Writing , the second XOR with cancels the first, leaving .
QuestionA message is XORed with a key stream, and the same key stream is reused for a second message. Why is this unsafe?Show answer
Because the two ciphertexts XORed together cancel the key, giving . An attacker who sees both ciphertexts learns the relation between the two plaintexts without ever knowing the key. A one-time pad needs a fresh key for every message.