Module · Foundational Mathematics
XOR Exclusive OR
A single-bit operation that quietly powers every stream cipher, every one-time pad, and the inner machinery of every block cipher you will ever use. Learn it once and it never leaves you.
The Rule, In One Sentence
One OR the other, but not both.
XOR is short for Exclusive OR. It returns true (1) when exactly one of its two inputs is true, and false (0) when the inputs match. The symbol is ⊕, a circle with a plus inside, and it is the most important operator in symmetric cryptography.
Distinction
OR is generous. XOR is strict.
The two operations agree on three out of four cases. Their disagreement is what gives XOR its name and its cryptographic power.
Inclusive OR
Symbol: ∨ · "either, or both"
Coffee OR tea? Both is fine.
Exclusive OR
Symbol: ⊕ · "one or other, not both"
Window XOR aisle seat. Pick one.
Truth Table
The Whole Operation, On One Grid
Read the table by picking your first bit from the left column, your second bit from the top row, and finding where they meet. Click any cell to read what it means. Cells where the bits match return 0. Cells where they differ return 1.
Try It
Flip the Bits Yourself
Click either bit to toggle it between 0 and 1. The result panel updates instantly. Notice how the result is 1 only when the two bits are different.
Tip: press A or B on your keyboard to toggle each bit.
From Bits To Bytes
XOR a Whole Byte at a Time
Real data is not a single bit. It is bytes, kilobytes, gigabytes. To XOR a byte, simply XOR each pair of bits in the same column. Stack the values, line them up, and apply the truth table eight times.
The Cryptographic Magic
Why XOR is the Heart of Symmetric Encryption
XOR has a quiet superpower: applying the same key twice gives you back the original message. The encryption operation and the decryption operation are literally the same operation. No inverse function needed.
Start with plaintext and a key
You have a message you want to hide (the plaintext) and a secret value known only to sender and receiver (the key). Both are sequences of bits.
XOR them to encrypt
The sender XORs each plaintext bit with the matching key bit. The result is the ciphertext, which looks like random noise to anyone without the key.
XOR again to decrypt
The receiver applies XOR with the same key. Because XOR is self-inverse, the key cancels itself out and the original plaintext appears.
The same operation runs both directions
This is why ChaCha20, RC4, AES-CTR, and the one-time pad all share the same final step: XOR the data with a keystream. Encryption and decryption are the identical operation. The only thing that has to be secret is the key.
Reference
Properties Worth Memorizing
These four identities show up in cryptanalysis, hash construction, parity checking, and almost every cipher proof you will read. Keep them close.
Identity
XOR with zero leaves the value unchanged. Zero is the do-nothing key.
Self-Inverse
XOR a value with itself and you always get zero. This is what makes the "decrypt by re-encrypting" trick work.
Commutative
The order of operands does not matter. Useful when reordering equations during analysis.
Associative
Grouping does not matter either. You can XOR a stream of values in any order and get the same final result.