3. Hamming Codes

Find and fix exactly which bit flipped

In 1950, Richard Hamming was working at Bell Labs alongside the massive relay computers of the era. Every weekend, the machines would run long calculations unattended — and every Monday, Hamming would arrive to find they had halted mid-computation because a single bit error had tripped the parity check. The machine knew something was wrong, but not what. It just stopped.

Hamming was frustrated. "If the machine can detect an error, why can't it figure out where the error is and correct it?" That question led him to a breakthrough: by placing parity bits at cleverly chosen positions, you can not only detect that an error occurred, but pinpoint its exact location and flip the corrupted bit back. Hamming codes were the first practical error-correcting codes, and they remain one of the most elegant constructions in all of information theory.

The Key Idea: Overlapping Parity Groups

With simple parity (page 1), we used a single parity bit covering all data bits. That tells us whether an error occurred, but not where. Hamming's insight was to use multiple parity bits, each covering a different overlapping subset of data bits.

Think of a Venn diagram with three overlapping circles. Each circle represents one parity group. Every data bit sits in a unique combination of circles — some are in circle A only, some in A and B, some in all three, and so on. When an error occurs, the pattern of which parity checks fail (the syndrome) tells you exactly which region of the diagram the error is in — and each region corresponds to exactly one bit position.

Core principle: With r parity bits you get 2r possible syndrome values. One syndrome (all zeros) means "no error." The remaining 2r − 1 syndromes each point to one bit position. So r parity bits can protect up to 2r − 1 total bits.

Hamming(7,4): The Classic Code

The most famous Hamming code encodes 4 data bits using 3 parity bits, for a total codeword length of 7 bits. Its code rate is 4/7 ≈ 0.571 — far better than the 1/3 rate of triple repetition, and it can still correct any single-bit error.

The 7 bit positions are numbered 1 through 7. Parity bits occupy the powers of two: positions 1, 2, and 4. Data bits fill the remaining positions: 3, 5, 6, and 7.

Why powers of two? Write each position number in binary:

PositionBinaryRole
1001Parity bit p1
2010Parity bit p2
3011Data bit d1
4100Parity bit p4
5101Data bit d2
6110Data bit d3
7111Data bit d4

Each parity bit checks every position whose binary representation has a 1 in that parity bit's column:

p1 (position 1) checks positions with bit pattern ***1: positions 1, 3, 5, 7

p2 (position 2) checks positions with bit pattern **1*: positions 2, 3, 6, 7

p4 (position 4) checks positions with bit pattern *1**: positions 4, 5, 6, 7

Notice that every data position appears in a unique combination of parity groups. Position 3 (011) is checked by p1 and p2. Position 5 (101) is checked by p1 and p4. Position 6 (110) is checked by p2 and p4. Position 7 (111) is checked by all three. This uniqueness is exactly what lets the syndrome pinpoint the error.

Encoding: Worked Example

Suppose we want to encode the four data bits: d1=1, d2=0, d3=1, d4=1.

Step 1 — Place data bits:

Position:   1   2   3   4   5   6   7

Contents:   p1   p2   1   p4   0   1   1

Step 2 — Calculate p1 (covers positions 1, 3, 5, 7):

Bits at positions 3, 5, 7 = 1, 0, 1.   XOR = 1 ⊕ 0 ⊕ 1 = 0.   So p1 = 0.

Step 3 — Calculate p2 (covers positions 2, 3, 6, 7):

Bits at positions 3, 6, 7 = 1, 1, 1.   XOR = 1 ⊕ 1 ⊕ 1 = 1.   So p2 = 1.

Step 4 — Calculate p4 (covers positions 4, 5, 6, 7):

Bits at positions 5, 6, 7 = 0, 1, 1.   XOR = 0 ⊕ 1 ⊕ 1 = 0.   So p4 = 0.

Result — Encoded codeword:

Position:   1   2   3   4   5   6   7

Codeword:   0   1   1   0   0   1   1

We transmit 0110011. The four data bits (1, 0, 1, 1) are embedded at positions 3, 5, 6, 7, and the three parity bits (0, 1, 0) make every parity group have even parity.

Decoding: Finding the Error

Suppose the received word has an error at position 5. The bit there flips from 0 to 1, so we receive 0110111.

Check p1 (positions 1, 3, 5, 7): 0 ⊕ 1 ⊕ 1 ⊕ 1 = 1   (FAIL)

Check p2 (positions 2, 3, 6, 7): 1 ⊕ 1 ⊕ 1 ⊕ 1 = 0   (OK)

Check p4 (positions 4, 5, 6, 7): 0 ⊕ 1 ⊕ 1 ⊕ 1 = 1   (FAIL)

The syndrome is formed by reading the check results from p4 down to p1: 1, 0, 1  →  1012 = 5.

The syndrome says the error is at position 5. Flip bit 5 back from 1 to 0, and we recover the original codeword 0110011.

Elegant efficiency: The syndrome is literally the binary address of the bad bit. No searching, no trial-and-error — you compute three XORs and read off the answer. This is why the parity bits sit at powers of two: they correspond to the individual binary digits of each position number.

SECDED: Single Error Correct, Double Error Detect

Basic Hamming(7,4) corrects single errors perfectly, but what happens with two errors? The syndrome will be nonzero and will point to some position — but the wrong position. Correcting it would make things worse. We need a way to distinguish "one error" from "two errors."

The solution is simple: add one more parity bit, p0, at position 0, covering all bits (including itself). This gives us Hamming(8,4), often called SECDED — Single Error Correction, Double Error Detection.

Syndrome = 0, overall parity OK → No error.

Syndrome ≠ 0, overall parity WRONG → Single error. The syndrome gives its position. Correct it.

Syndrome ≠ 0, overall parity OK → Double error. Detected but uncorrectable. Signal for retransmission.

This SECDED scheme is the basis for ECC RAM — the error-correcting memory used in every server and workstation where data integrity matters.

Hamming Distance

The Hamming distance between two binary strings is the number of positions where they differ. For example, 1011 and 1110 differ in positions 2, 3, and 4, so their Hamming distance is 3.

The minimum distance (dmin) of a code is the smallest Hamming distance between any two valid codewords. This single number determines what the code can do:

Correction: A code can correct up to t errors if dmin ≥ 2t + 1.

Detection: A code can detect up to t errors if dmin ≥ t + 1.

For Hamming(7,4), dmin = 3. So it can correct 1 error (2×1 + 1 = 3) or detect up to 2 errors (2 + 1 = 3).

For SECDED Hamming(8,4), dmin = 4. It can correct 1 error and detect 2 errors simultaneously.

The Hamming Bound

How efficient can a single-error-correcting code be? Consider a code with n total bits and k data bits that corrects t errors. There are n − k parity bits producing 2n−k possible syndromes. Each correctable error pattern needs its own syndrome, plus we need one syndrome for "no error."

For single-error correction (t = 1), the number of patterns is: 1 (no error) + n (one error in each position). So we need:

1 + n ≤ 2n − k

A code that achieves this bound with equality is called a perfect code — every syndrome is used, nothing is wasted.

Hamming(7,4):

1 + 7 = 8 = 27 − 4 = 23 = 8  —  Perfect!

Every one of the 8 syndromes is used: one for "no error" and seven for "error at position 1" through "error at position 7."

In general, Hamming codes exist for any r ≥ 2: Hamming(2r−1, 2r−1−r). They are always perfect single-error-correcting codes.

Extended Hamming in Practice

Hamming codes and their SECDED extensions are used wherever fast, lightweight single-error correction is needed:

ApplicationCodeParameters
ECC RAM SECDED Hamming(72,64) — 64 data bits, 8 check bits
Early computer memory Hamming(7,4) Used in IBM mainframes from the 1950s–60s
Satellite communications Hamming (building block) Combined with interleaving for burst error protection

ECC RAM is ubiquitous in servers. Each 64-bit memory word is stored as 72 bits. The 8 extra bits implement SECDED: any single-bit flip is silently corrected, and any double-bit error is detected and reported. The overhead is only 12.5%.

From Bits to Symbols

Hamming codes are elegant, but they operate on individual bits. In practice, errors often arrive in bursts — a scratch on a CD corrupts thousands of consecutive bits, a lightning strike garbles a sequence of radio symbols, a failing memory cell affects an entire byte.

A burst of even two bit errors in the same codeword exceeds what Hamming(7,4) can correct. We could interleave multiple Hamming codewords to spread burst errors across them, and that helps — but there is a fundamentally better approach.

Reed-Solomon codes work on multi-bit symbols rather than individual bits. A single symbol error — no matter how many bits within it are wrong — counts as just one error to correct. This makes Reed-Solomon naturally suited to burst errors. It is the code behind CDs, DVDs, QR codes, and deep-space communication — and it is the subject of the next page.

Key Takeaways

  • Hamming codes use multiple overlapping parity groups so that the pattern of failures (the syndrome) directly identifies the corrupted bit.
  • Parity bits occupy power-of-two positions because each one corresponds to a single binary digit of the position number.
  • Hamming(7,4) encodes 4 data bits into 7 total bits (rate 4/7) and corrects any single-bit error — far more efficient than triple repetition.
  • Adding an overall parity bit gives SECDED (Hamming(8,4)): correct 1 error and detect 2 errors. This is the basis of ECC RAM.
  • Hamming codes are perfect codes — they use every available syndrome, achieving the Hamming bound with equality.
  • Hamming codes work at the bit level. For burst errors that corrupt many adjacent bits, Reed-Solomon codes operating on multi-bit symbols are the next step up.

Hamming showed the world that error correction does not require brute-force repetition — it requires structure. By placing parity bits at powers of two, he turned the syndrome into a binary address, making correction as simple as a lookup. But the real world throws burst errors, not isolated bit flips. Next, we will see how Reed-Solomon codes extend the idea from individual bits to multi-bit symbols, enabling the error correction that protects everything from QR codes to interplanetary communication.

The Error Correction Ladder

Parity → detects 1 error

Repetition → corrects errors, but wastes bandwidth

CRC → detects many errors, cannot correct

Hamming → corrects 1 error efficiently (you are here)

Reed-Solomon → corrects burst errors on symbols (next)

Try It Yourself — Hamming(7,4) Encoder/Decoder

Part 1 — Encoding

Toggle the four data bits to see how the Hamming(7,4) codeword is constructed.

Part 2 — Error Simulation

Click any bit below to simulate a single-bit error. The decoder will compute the syndrome and locate the error.