Search

Search papers and lessons

Exclusive Or (XOR)

Level 1 · 10 min read

Exclusive or (XOR) outputs 1 when its two bits differ and 0 when they match; applied to whole numbers bit by bit, it is a cheap, reversible way to mix or compare data.

On this page

  • Colour = topic mix
  • Discrete math
  • Systems
  • Electrical engineering
  • ReadReady to readNot yet

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, aa and bb. The operation is written a⊕ba \oplus b (read "a XOR b"), and the rule is short: the output is 1 when exactly one input is 1.

  1. Choose the inputs. Let a=1a = 1 and b=0b = 0. Check: each input is a single bit, 0 or 1.
  2. Compare the bits. The bits differ, since one is 1 and the other is 0. Check: 1≠01 \neq 0.
  3. Output 1. Because the inputs differ, the output is 1. So 1⊕0=11 \oplus 0 = 1. Check: the full set of four input pairs is listed in the truth table in The math section.
  4. Apply it to whole numbers. Take the 4-bit binary numbers 13=110113 = 1101 and 6=01106 = 0110. Compare bit by bit, left to right: 1⊕0=11 \oplus 0 = 1, 1⊕1=01 \oplus 1 = 0, 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1. The result is 10111011, which is 11. Check: in each column the two bits differ or match, and the output follows the rule.
  5. Undo it. XOR the result with 66 again: 1011⊕0110=11011011 \oplus 0110 = 1101, 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 ∧\land is AND, ∨\lor is OR and ¬\lnot 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:

a⊕b=(a∧¬b)∨(¬a∧b).a \oplus b = (a \land \lnot b) \lor (\lnot a \land b).

Read it as: the output is 1 when aa is 1 and bb is 0, or when aa is 0 and bb is 1.

The full truth table has four rows:

aabba⊕ba \oplus b
000
011
101
110

Step 3 in symbols: the rule for one pair

For a=1a = 1, b=0b = 0, the first term is a∧¬b=1∧1=1a \land \lnot b = 1 \land 1 = 1, and the second term is ¬a∧b=0∧0=0\lnot a \land b = 0 \land 0 = 0. The OR of the two is 11.

Step 4 in symbols: bitwise on whole numbers

For nn-bit numbers xx and yy, x⊕yx \oplus y is the number whose ii-th bit is xi⊕yix_i \oplus y_i. 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:

  • a⊕a=0a \oplus a = 0, because two equal bits match.
  • a⊕0=aa \oplus 0 = a, because 0 changes nothing.

XOR can be regrouped freely: it is associative, meaning (a⊕b)⊕c=a⊕(b⊕c)(a \oplus b) \oplus c = a \oplus (b \oplus c). It is also commutative, meaning the order of the two inputs does not matter, so a⊕b=b⊕aa \oplus b = b \oplus a. Together these give (a⊕b)⊕b=a⊕(b⊕b)=a⊕0=a(a \oplus b) \oplus b = a \oplus (b \oplus b) = a \oplus 0 = a.

So for any key kk, (m⊕k)⊕k=m(m \oplus k) \oplus k = m.

Parity

For more than two bits, a⊕b⊕ca \oplus b \oplus c 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, x⊕yx \oplus y 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

Example
Example 1: the truth table, tiny

Find 1⊕11 \oplus 1 and 0⊕10 \oplus 1 using the rule that the output is 1 when the inputs differ.

  1. 1⊕11 \oplus 1: the inputs are both 1, so they match. The output is 0.
  2. Check against the table: row a=1a = 1, b=1b = 1 gives 0.
  3. 0⊕10 \oplus 1: the inputs are 0 and 1, which differ. The output is 1.
  4. Check against the table: row a=0a = 0, b=1b = 1 gives 1.
  5. 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.
Example
Example 2: 4-bit words, the standard case

Compute 1010⊕01101010 \oplus 0110 bit by bit, then check the result by XORing it with 01100110.

  1. Column 1 (leftmost): 1⊕0=11 \oplus 0 = 1.
  2. Column 2: 0⊕1=10 \oplus 1 = 1.
  3. Column 3: 1⊕1=01 \oplus 1 = 0.
  4. Column 4: 0⊕0=00 \oplus 0 = 0.
  5. Result: 11001100. In decimal, 1100=121100 = 12, and 10⊕6=1210 \oplus 6 = 12.
  6. Check: 1100⊕01101100 \oplus 0110. Column by column, 1⊕0=11 \oplus 0 = 1, 1⊕1=01 \oplus 1 = 0, 0⊕1=10 \oplus 1 = 1, 0⊕0=00 \oplus 0 = 0, giving 10101010, which is the original 10.
  7. Interpretation: the XOR of two numbers and one of them gives the other. This is the undo property in action.
Example
Example 3: parity for a lost disk, realistic

A three-disk array stores data on disk A and disk B, and parity on disk P. Disk A holds 10101010 and disk B holds 01110111. Find the parity, then recover disk B after it fails.

  1. The parity is the XOR of the two data blocks: P=1010⊕0111P = 1010 \oplus 0111.
  2. Column by column: 1⊕0=11 \oplus 0 = 1, 0⊕1=10 \oplus 1 = 1, 1⊕1=01 \oplus 1 = 0, 0⊕1=10 \oplus 1 = 1. So P=1101P = 1101.
  3. Check: 1101⊕10101101 \oplus 1010 should give 01110111, which is disk B. Column by column: 1⊕1=01 \oplus 1 = 0, 1⊕0=11 \oplus 0 = 1, 0⊕1=10 \oplus 1 = 1, 1⊕0=11 \oplus 0 = 1, giving 01110111.
  4. Disk B fails. Recover it from the parity and disk A: B=P⊕A=1101⊕1010B = P \oplus A = 1101 \oplus 1010.
  5. The result is 01110111, the original contents of disk B.
  6. 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 10011001 and the parity block is 11001100. Disk B has failed. What was on disk B?

Answer: B=P⊕A=1100⊕1001B = P \oplus A = 1100 \oplus 1001. Column by column: 1⊕1=01 \oplus 1 = 0, 1⊕0=11 \oplus 0 = 1, 0⊕0=00 \oplus 0 = 0, 0⊕1=10 \oplus 1 = 1. So disk B held 01010101.

Common mistakes and tradeoffs

It is tempting to think XOR is just OR. In fact they differ on one row: OR gives 1 for 1∨11 \lor 1, while XOR gives 0 for 1⊕11 \oplus 1, 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 c1⊕c2=m1⊕m2c_1 \oplus c_2 = m_1 \oplus m_2, 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 1⊕1=01 \oplus 1 = 0 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 22 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 a⊕a=0a \oplus a = 0 and a⊕0=aa \oplus 0 = a.
  • 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 1⊕01 \oplus 0, 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 1101⊕01011101 \oplus 0101 bit by bit.Show answer

Column by column: 1⊕0=11 \oplus 0 = 1, 1⊕1=01 \oplus 1 = 0, 0⊕0=00 \oplus 0 = 0, 1⊕1=01 \oplus 1 = 0. The result is 10001000, which is 8.

QuestionWhy does applying the same XOR key twice return the original message?Show answer

Because a⊕a=0a \oplus a = 0 and a⊕0=aa \oplus 0 = a. Writing (m⊕k)⊕k(m \oplus k) \oplus k, the second XOR with kk cancels the first, leaving m⊕0=mm \oplus 0 = m.

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 c1⊕c2=m1⊕m2c_1 \oplus c_2 = m_1 \oplus m_2. 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.

Sources