Every file you download, every photo in your phone and every byte in a memory chip lives with a small risk: a bit flips. A stray cosmic particle, a weak voltage, a scratch on a disc. One flipped bit can turn a 0 into a 1 and a "5" into a "7". The clever part is that a machine can notice and repair the damage without ever seeing the original.

A frustrated weekend

In the late 1940s Richard Hamming was using the Bell Model V relay computer at Bell Labs, which read its programs from punched paper tape. On weekdays operators watched the machine and fixed errors when it stopped. At weekends nobody was there, so when it hit an error it simply moved on to the next job. Hamming, who worked weekends, kept coming back to a dead run and having to start from scratch. His question was a good one: if the machine can tell something is wrong, why can't it work out what is wrong and fix it? He published the answer in 1950.

Detecting is easy, locating is clever

The simplest check is a parity bit: add one extra bit so that the number of 1s is even. If one bit flips, the count turns odd and you know something broke. But you do not know where. Hamming's trick was to use several parity bits, each watching a different overlapping group of positions. The pattern of which checks fail then spells out the address of the broken bit.

In the classic Hamming(7,4) code, 4 data bits are stretched into 7 by adding 3 parity bits. Number the positions 1 to 7. The parity bits sit at positions 1, 2 and 4 (the powers of two) and the data bits at 3, 5, 6 and 7. Each parity bit covers every position whose binary number has its own bit switched on:

checkcovers positions
p11 3 5 7
p22 3 6 7
p44 5 6 7

Position 5 is binary 101, so it is watched by p1 and p4 but not p2. Position 6 is 110, watched by p2 and p4. Every position has its own unique set of watchers, which is exactly what makes the address readable.

Break it yourself

Set the four data bits, then tap any bit of the 7-bit codeword to corrupt it. Watch which checks fail.

1 · choose the 4 data bits (tap)
2 · the bits that arrive: tap any bit to flip it
3 · the three checks

Reading the address

Write the failing checks as a binary number, with p4 as the leftmost digit: failed means 1, passed means 0. That number is the position of the flipped bit. If p1 and p4 fail while p2 passes, you get 101, which is 5, so bit 5 is wrong. Flip it back and the message is whole again. If nothing fails, the code word arrived intact. This number is called the syndrome.

It even works when the flipped bit is itself a parity bit. If only p2 fails, the syndrome is 010, which is 2, and position 2 is the p2 bit itself.

Where it runs out

Hamming(7,4) has a minimum distance of 3: any two valid codewords differ in at least 3 positions. That is enough to correct one flipped bit, or to detect two, but not to do both at once. Try the "Flip two bits" button. The checks still fail, the syndrome still points somewhere, and the decoder confidently "repairs" a bit that was fine, leaving three bits wrong. Adding one more overall parity bit gives the extended 8-bit version, which corrects any single error and detects (without fixing) a double error. Codes of this kind are used in error-correcting server memory.

The price is overhead. Here 3 of every 7 bits are spent on protection, so only 4/7 of the channel carries your message. Longer Hamming codes are far more economical: in the same family, 4 check bits protect 11 data bits and 5 protect 26. Bigger blocks mean cheaper insurance per bit, but they also mean more chance of two errors landing in one block.

Interactive: the four data bits fix the three parity bits by the rules in the table. Flipping bits in the received row does not change what was sent, so the decoder is working only from what arrived.