Part III · Chapter 10

Hamming’s Self-Correcting Code

Four data bits, three check bits, and three overlapping circles. Flip any one of the seven bits and the failing checks point straight at it, like a binary address. It is the twenty-questions game, played against noise.

In the late 1940s Richard Hamming, a mathematician at Bell Labs, worked with relay computers that checked their own data with error-detecting codes. When a check failed, the machine could tell that something was wrong but not what, so it stopped the job. Hamming later recalled his frustration: if a machine can detect an error, why can't it work out where the error is and fix it? The codes he devised in answer were published in 1950, and their central trick is still used today, for example in the error-correcting memory of many servers.

Chapter 9 ended with a hint: locating one error among a handful of positions is a twenty-questions problem, and the parity grid asked more questions than it needed to. Hamming's code asks the minimum.

Three circles

Draw three overlapping circles. They cut the plane into seven regions. Put the four data bits in the four regions where circles overlap, and one check bit in each circle's private region. Choose each check bit so that its whole circle contains an even number of 1s. That is the entire encoder.

Now play the noise. Click any region (or use the buttons) to flip a received bit. Each circle checks its own parity; the circles that fail glow coral. There is exactly one region that lies inside every failing circle and outside every passing one: that is the culprit.

Interactive 10.1Three Circles
Set the data, then flip any received bit
data bits (overlaps) check bits (private regions) and passing circles flipped bits and failing circles the bit the decoder will flip
Data to send:
Noise (flip a received bit):
 
Flip any single bit, data or check, and press Correct it: the original word comes back every time. Now flip two bits. The circles still point confidently at one region, but it is a third, innocent bit. "Correcting" it makes three wrong bits, and the decoder hands over a different valid codeword: wrong data, with no warning.
Hamming(7,4)

Seven bits carry four bits of data; three overlapping parity checks locate and fix any single flipped bit in the block.

The name Hamming(7,4) says it all: blocks of 7 bits, 4 of them data, rate 4/7 ≈ 0.57. Compared with the triple-repetition code (rate 1/3), Hamming sends 4 data bits in 7 bits instead of 12, and still fixes any single error per block.

Two errors fool it

Hamming(7,4) corrects any one error per block. Two errors in the same block are always "corrected" into a wrong word, because the decoder assumes one error and blames the bit the checks point to.

The checks spell out an address

Here is the same code drawn a second way, and it reveals why the circles work. Number the seven positions 1 to 7 and write each number in binary. Put the check bits at positions 1, 2 and 4, the powers of two. Check 1 covers every position whose binary number has a 1 in the ones place; check 2 covers those with a 1 in the twos place; check 4 those with a 1 in the fours place. (These are exactly the circles above.)

Flip the bit at position 5 (binary 101). It sits under check 4 and check 1, but not check 2. So checks 4 and 1 fail, and reading the failures as binary digits, 1 0 1, spells 5. The pattern of failing checks, called the syndrome, is the address of the error. The two views below share one received word: change it in either and both update.

Interactive 10.2The Syndrome Address
Click a position to flip it
data positions 3, 5, 6, 7 check positions 1, 2, 4 flipped bits and failing checks
 
Every position from 1 to 7 has a different binary number, and "no error" gets 000. Three checks give 2 × 2 × 2 = 8 possible syndromes: exactly enough for "no error" plus seven single-error positions, with none to spare. Codes that fit this snugly are called perfect.

The recipe scales. With m checks you get 2m syndromes, enough to address 2m − 1 positions. Four checks make Hamming(15,11), rate 11/15 ≈ 0.73; five make Hamming(31,26), rate ≈ 0.84; six make Hamming(63,57), rate ≈ 0.90. The check bits grow only like the logarithm of the block length, which is Chapter 1's halving argument again: naming one of N positions takes about log₂ N yes-or-no answers.

But longer blocks catch more errors, and each block still survives only one. That trade, efficiency against exposure, is what the last figure measures.

Interactive 10.3The Code Race
Slide the noise; send a batch to compare with the exact values
chance a decoded data bit is wrong (exact) measured in your batch
1.0%
 
At p = 1%, Hamming(7,4) cuts the data error rate roughly tenfold, at a much better rate than repetition. Hamming(15,11) has an even better rate but suffers more, because a 15-bit block more often catches two errors. Raise the noise towards 10% and the Hamming codes struggle while repetition holds up better, at its punishing rate. No code here escapes the tug-of-war between rate and reliability. Chapter 11 asks how far that tug-of-war can be won.

Beyond Hamming

Adding one more overall parity bit to a Hamming code gives a code that corrects any single error and detects (without fixing) any double error, a combination often abbreviated SECDED and widely used in error-correcting computer memory. Other families go further: Reed–Solomon codes, which work on whole bytes rather than single bits, protect CDs, DVDs and QR codes and can repair long bursts of damage. And modern codes, discussed in the next chapter, come remarkably close to the ultimate limit.

Intuition Check

Two bits in the same Hamming(7,4) block are flipped. What does the decoder do?

Intuition Check

Why are three check bits exactly enough to correct one error in a 7-bit block?

Connections

What you now see

  • Hamming(7,4) places 4 data bits and 3 check bits in three overlapping circles, each with even parity.
  • The failing checks, read as binary digits, spell the position of a single flipped bit: the syndrome is an address.
  • Any single error per block is corrected; two errors are "corrected" into a wrong word without warning.
  • m check bits can address 2m − 1 positions, so longer Hamming codes have better rates but more exposure to double errors.
  • Every code so far trades rate for reliability. Next: the limit of that trade.