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.
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.
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.
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.
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.
Two bits in the same Hamming(7,4) block are flipped. What does the decoder do?
Why are three check bits exactly enough to correct one error in a 7-bit block?
Connections
- Chapter 1: the syndrome is a twenty-questions answer, three yes/no checks naming one of eight situations.
- Computer Science, Chapter 4: Remembering a Moment builds memory from switches, the hardware that error-correcting memory protects.
- The logic book covers XOR, the "odd number of 1s" operation that every parity check computes.
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.