A survey of how XOR, shifts, rotations, AND, OR, and permutations form the universal building blocks of every modern encryption algorithm.
Every encryption algorithm, from the simplest XOR cipher to the most complex post-quantum scheme, is built from the same small set of bitwise operations. I have worked with AES, ChaCha20, Twofish, and even toy ciphers for teaching, and the pattern is always the same: XOR for combining, shifts and rotations for diffusion, AND and OR for nonlinear substitution, and permutations for rearrangement. Understanding these building blocks lets you reason about any cipher.
The reason this set of operations is universal comes down to Shannon's principles of confusion and diffusion. Confusion makes the relationship between key and ciphertext complex — this is where S-boxes and AND/OR operations come in. Diffusion spreads the influence of each plaintext bit across many ciphertext bits — this is the job of XOR, shifts, and permutations. A cipher without both is breakable.
I once benchmarked AES-NI hardware acceleration against a software implementation of the same algorithm. The hardware version processed data 50x faster, and understanding the bit-level operations under the hood explained exactly why specialized silicon makes such a difference.
XOR appears in every single modern encryption algorithm. It is the default tool for combining plaintext with a key or keystream because it is the only bitwise operation that is both invertible and balanced. A cipher without XOR is like a car without wheels — it simply cannot work.
| Algorithm | XOR Usage |
|---|---|
| AES | AddRoundKey: XOR state with round key every round |
| ChaCha20 | Final step: XOR keystream with plaintext |
| Blowfish / Twofish | Whitening: XOR input/output with subkeys |
| Serpent | Key mixing: XOR between every round |
| Salsa20 | Quarter round: XOR after addition/rotation |
| RSA (asymmetric) | No direct XOR — uses modular exponentiation instead |
The XOR operation at the heart of these ciphers is identical to what our bitwise calculator does. Every time you click the XOR button, you are executing the same operation that protects billions of HTTPS connections. This connection between a simple calculator and world-class cryptography is something I find genuinely fascinating.
Bit shifts and rotations spread the influence of each input bit across the output. A single bit change fed through a rotation will affect different bit positions in subsequent rounds, creating the avalanche effect. Every serious cipher uses rotations or shifts for diffusion.
The specific rotation distances are carefully chosen. ChaCha20's rotations (16, 12, 8, 7) are all distinct and pairwise coprime with 32, ensuring that repeated application cycles through all bit positions. AES's ShiftRows distances (1, 2, 3) guarantee that every column of the state is mixed with every other column over successive rounds.
XOR alone is linear in GF(2), meaning a cipher built only from XOR and shifts could be broken with linear cryptanalysis. The nonlinearity comes from AND and OR operations, or from the S-box lookup tables that are themselves constructed from AND, OR, and XOR in GF(2^8).
// S-box construction patterns across ciphers:
// AES S-box: multiplicative inverse + affine transform
// The affine transform uses AND, XOR, and shifts:
output_bit[i] = input_bit[i] XOR
input_bit[(i+4)%8] XOR
input_bit[(i+5)%8] XOR
input_bit[(i+6)%8] XOR
input_bit[(i+7)%8] XOR
constant_bit[i]
// Twofish: uses two S-boxes built from GF(2^8) inversion
// plus a PHT (pseudo-Hadamard transform) using modular addition
// All built from XOR and AND at the lowest level
// DES S-box: 6-bit to 4-bit lookup, row = (bit0, bit5), col = (bit1..bit4)
// Designed to maximize nonlinearity (minimize linear approximation probability)
The mathematical property that makes AND useful in S-boxes is that it introduces nonlinearity. In GF(2), AND corresponds to multiplication (a AND b = a * b mod 2), which is the only nonlinear operation in the field. This is why AND-based operations are essential for resisting linear cryptanalysis.
Permutations shuffle bits or bytes without changing their values. DES uses a bit-level permutation (P-box) that rearranges the 32 output bits of the S-box operation. AES uses a byte-level permutation (ShiftRows). The principle is the same: ensure that the same bit positions are not processed together round after round.
Bit-level permutations (like DES's IP and P-box) are expensive in software but cheap in hardware. Byte-level permutations (like AES's ShiftRows) strike a balance — they are fast in both hardware and software. Modern ciphers tend to favor byte or word-level operations because CPUs are optimized for these data sizes.
Different ciphers use different proportions of these operations. Here is a comparison of the bitwise "recipe" for several major algorithms:
| Algorithm | XOR | Shift/Rotate | AND/OR | Permutation | Addition |
|---|---|---|---|---|---|
| AES | Heavy (every round) | Moderate (ShiftRows) | Moderate (S-box via affine) | Byte-level | None |
| ChaCha20 | Heavy (quarter round) | Heavy (4 rotations) | None | Word-level (quarter round) | Modular 32-bit |
| DES | Heavy (Feistel XOR) | Heavy (key schedule) | Heavy (S-box) | Bit-level (IP, P-box) | None |
| Twofish | Heavy (whitening) | Moderate (PHT) | Heavy (S-box) | Byte-level | Modular |
| Salsa20 | Heavy (quarter round) | Heavy (3 rotations) | None | Word-level (quarter round) | Modular 32-bit |
Notice that ARX ciphers (ChaCha20, Salsa20) use only Addition, Rotation, and XOR — no AND, OR, or lookup tables. This makes them extremely fast in software and resistant to timing attacks. Block ciphers (AES, DES, Twofish) use S-box lookup tables for stronger per-round nonlinearity but at the cost of more complex implementation.
The fundamental difference between stream and block ciphers is how they use bitwise operations. Stream ciphers generate a pseudorandom keystream and XOR it with the plaintext — the bitwise operation is simple and uniform. Block ciphers apply multiple rounds of substitution and permutation to fixed-size blocks, then chain blocks together using XOR in modes like CBC or CTR.
// Stream cipher (ChaCha20): generate keystream, XOR with plaintext keystream = chacha20_block(key, nonce, counter); ciphertext = plaintext ^ keystream; // Single XOR per block // Block cipher (AES-CBC): encrypt block, XOR with previous ciphertext C[0] = AES_encrypt(P[0] ^ IV) // Initial block: XOR with IV C[1] = AES_encrypt(P[1] ^ C[0]) // Subsequent: XOR with previous ciphertext
CTR mode blurs the line between stream and block ciphers. It uses a block cipher to encrypt counter values, then XORs the output with the plaintext — functionally identical to a stream cipher. The same AES round operations are used, but the XOR operation is moved to the final step.
XOR is the single most important bitwise operation in all of cryptography. Use our bitwise calculator to experiment with it — enter any two hex values and see the result. This is exactly the operation happening billions of times per second in HTTPS connections worldwide.
Every modern encryption algorithm uses XOR as the core combining operation. Most also use bitwise shifts and rotations for diffusion, AND and OR for substitution layer computations, and permutation operations to rearrange bits or bytes. These four operation types — XOR, shift/rotate, AND/OR, and permutation — appear in every major cipher from AES to ChaCha20 to Twofish.
XOR is universal because it is the only fundamental bitwise operation that is invertible (a XOR b XOR b = a), produces balanced output (50% ones, 50% zeros when the key is random), is linear in GF(2) which allows mathematical analysis, and is extremely cheap to implement in hardware and software. No other single bitwise operation has all these properties.
A stream cipher generates a pseudorandom keystream and XORs it with the plaintext, operating on one byte or bit at a time. A block cipher processes fixed-size chunks (typically 128 bits) through multiple rounds of substitution and permutation, then combines the result with the next block using XOR in a chaining mode. At the bit level, stream ciphers are simpler but require careful keystream management.
Confusion means the relationship between the key and ciphertext is complex — achieved through S-box substitutions (bit-level affine transforms) and key-dependent operations. Diffusion means changing one bit of plaintext changes roughly half the bits of ciphertext — achieved through XOR, shifts, and permutation operations that spread the influence of each input bit across the entire output.
Most asymmetric (public-key) algorithms like RSA and ECC use modular arithmetic and elliptic curve operations, not bitwise operations. However, they are typically used in hybrid encryption systems where the data itself is encrypted with a symmetric cipher (AES, ChaCha20) that does use bitwise operations. The symmetric component always relies on bitwise operations for performance.