TL;DR
An error-correcting code adds redundant bits to a message so that a receiver can notice, and often repair, bits that were flipped in transit or storage. The codewords are chosen to be far apart from one another, and the receiver picks the closest one. The linear codes studied here do this with simple arithmetic on bits. The Hamming(7,4) code adds three parity bits to four data bits, and it repairs any single flipped bit.
Builds on: Finite Field, Vector Space · Leads to: linear block codes, Linear Programming Bound
Where it's used
Computer memory. Many servers and workstations store extra check bits beside the data, so that a single bit flipped by electrical noise or a stray particle is corrected before the program reads it. Hamming codes are the classic design for this, and the same idea of check bits beside the data is used in larger codes today.
Printed QR codes. A QR code can still be scanned after a corner is torn or smudged. The printed symbol carries redundant data, so the scanner can rebuild what the damage hid.
Deep-space links. A spacecraft sends its data across a very noisy radio channel and cannot ask for a resend in time. The data are encoded before transmission so that the receiver on Earth can repair the errors that the noise causes.
How it works
The idea is easiest to follow with the Hamming(7,4) code. It takes four data bits and adds three parity bits, giving seven bits in total. Write the data as . The message is a string of bits, each 0 or 1. The parity bits are computed with addition modulo 2, the clock arithmetic on bits in which , so that 1 plus 1 wraps back to 0.
- Encode the message 1011. Here , , and . Compute the parity bits: , and . The codeword is 1011010. Check: each parity rule adds an even number of 1s once its own bit is included.
- Transmit, and suppose bit 3 flips. The receiver gets 1001010. Check: the third bit changed from 1 to 0, and nothing else changed.
- Compute three checks on the received word. Each check adds the bits at a fixed set of positions, modulo 2. Check 1 looks at positions 1, 2, 4 and 5: the bits are 1, 0, 1, 0, which sum to 2, so the check is 0. Check 2 looks at positions 1, 3, 4 and 6: the bits are 1, 0, 1, 1, which sum to 3, so the check is 1. Check 3 looks at positions 2, 3, 4 and 7: the bits are 0, 0, 1, 0, which sum to 1, so the check is 1. The three results form the syndrome .
- Match the syndrome to a position. The syndrome is exactly the pattern of checks that a single error in position 3 produces. So the receiver flips bit 3 back, giving 1011010.
- Read the data. The first four bits are the message, so the receiver recovers 1011. Check: this matches the message in step 1.
flowchart LR
A[Message 1011] --> B[Add three parity bits]
B --> C[Send 1011010]
C --> D[Noise flips bit 3]
D --> E[Compute the three checks]
E --> F{Checks all zero?}
F -->|no| G[Syndrome picks the bit to flip]
G --> H[Recover 1011]
F -->|yes| H
The math
A binary linear code of length and dimension is a linear code that is a subspace with codewords. The Hamming distance between two words is the number of positions in which they differ. The minimum distance of is the smallest Hamming distance between two different codewords. For a linear code, equals the smallest number of 1s in any nonzero codeword. Such a code is written . The Hamming(7,4) code is .
Step 1 in symbols: the encoder
Let be the message. The codeword is , where
and all sums are taken modulo 2. Step 1 of the walkthrough used , which gives , and .
The same encoder can be written as a matrix product with , where is the matrix with 1s on its diagonal and 0s elsewhere, and
Step 2 in symbols: the parity-check matrix
Let be the matrix whose rows are the three parity rules, with a 1 wherever a position takes part in that check:
A word is a codeword exactly when (mod 2). For the codeword 1011010, each row sums to an even number over its positions, so the product is zero. The rank of is 3, and the codewords form the kernel of , a subspace of dimension . That is why the code has codewords.
Step 3 in symbols: the syndrome
Let be the received word, where is the error pattern. The syndrome is . Since , it depends only on the error:
If has a single 1 in position , then is column of . The seven columns are , , , , , and . These are all seven nonzero vectors of , each appearing once. So every single error gives a different nonzero syndrome, and the syndrome tells the receiver which bit to flip.
Step 4 in symbols: correction and its limit
The decoder flips the bit at the position whose column equals . This corrects any single error. A code with minimum distance corrects up to errors whenever . Here , so . With two errors, the syndrome is nonzero, so the error is noticed, but it equals the sum of two columns. Because every nonzero vector is a column, that sum matches a third column, and the decoder flips a wrong bit.
Worked examples
Encode 0 as 000 and 1 as 111. The two codewords differ in all three positions, so the minimum distance is 3. Suppose the receiver gets 010.
- Count the 0s in 010. There are two 0s and one 1.
- Take the majority. The majority value is 0, so the decoder outputs 0.
- Interpretation: the single flipped bit, in position 2, was repaired. The code spends three bits to send one, which is a rate of 1/3. Hamming(7,4) is far more efficient, with a rate of 4/7.
Let , , and .
- Compute , which is 1 modulo 2.
- Compute , which is 0 modulo 2.
- Compute , which is 0 modulo 2.
- The codeword is 1101100.
- Check each row of : positions 1, 2, 4, 5 hold 1, 1, 1, 1, which sum to 4, so the check is 0. Positions 1, 3, 4, 6 hold 1, 0, 1, 0, which sum to 2, so the check is 0. Positions 2, 3, 4, 7 hold 1, 0, 1, 0, which sum to 2, so the check is 0. All checks pass.
- Interpretation: the parity rules produced a valid codeword, which is what the receiver expects to see with no errors.
A memory word holds the data 0110. Its parity bits are , and . The stored codeword is 0110110.
- A stray particle flips bit 5, which is (it was 1 and becomes 0). The read gives 0110010.
- Check 1 covers positions 1, 2, 4, 5: the bits are 0, 1, 0, 0, which sum to 1, so the check is 1.
- Check 2 covers positions 1, 3, 4, 6: the bits are 0, 1, 0, 1, which sum to 2, so the check is 0.
- Check 3 covers positions 2, 3, 4, 7: the bits are 1, 1, 0, 0, which sum to 2, so the check is 0.
- The syndrome is , which is column 5 of . Flip bit 5 back to recover 0110110.
- Interpretation: the program reads 0110 unharmed. The memory controller performs this check on each read, so the error is invisible to software.
Try it: The received word is 0110111. Find the syndrome, the bit to flip and the data. Answer: check 1 covers positions 1, 2, 4, 5 with bits 0, 1, 0, 1, sum 2, so 0. Check 2 covers positions 1, 3, 4, 6 with bits 0, 1, 0, 1, sum 2, so 0. Check 3 covers positions 2, 3, 4, 7 with bits 1, 1, 0, 1, sum 3, so 1. The syndrome is column 7, so flip bit 7 to get 0110110. The data are 0110.
Common mistakes and tradeoffs
It is tempting to think that adding more bits always means fewer errors. In fact the extra bits only help when they are arranged so that codewords are far apart. Random extra bits can leave two codewords one bit apart, and then a single flip can turn one valid message into another.
It is tempting to think that a code which corrects one error corrects any number. In fact correcting errors requires minimum distance at least . The Hamming(7,4) code has distance 3, so it corrects one error and fails silently on two.
Tradeoff: rate against distance. A code with more redundancy is more reliable but sends fewer useful bits per symbol. The repetition code has rate 1/3, and Hamming(7,4) has rate 4/7.
When to use it. Hamming codes suit settings where errors are rare and independent, such as a single flipped bit in memory. They are cheap to build and fast to decode.
When not to use it. When errors arrive in bursts, one code can lose several bits in a row. Designs for such channels spread the errors out or use stronger codes. The Hamming(7,4) code is too weak for that case.
History
Richard W. Hamming worked at Bell Labs in the late 1940s. The Wikipedia article on Hamming codes says he published the code in 1950, and it remains in use in applications such as ECC memory. Claude Shannon's theory of communication, in A Mathematical Theory of Communication (1948), set the other half of the picture. It showed that a channel has a limit, its capacity, beyond which no code can make the error rate arbitrarily small. Hamming's codes are simple and useful, but they are far from that limit. The search for codes that approach it occupied coding theory for decades.
Recap
- An error-correcting code adds redundancy so that codewords are far apart.
- A linear code over is a subspace, and its codewords are the kernel of a parity-check matrix .
- The syndrome depends only on the error, and for a single error it equals a column of .
- Minimum distance corrects errors when . Hamming(7,4) has , so it corrects one error.
- Read next: Linear Block Code for the general linear form, and Linear Programming Bound for limits on how good a code can be.