Introduction
A Hamming code can tell a receiver not only that a bit was damaged in transit, but which one. That sounds like it should need a lot of extra data. It needs three extra bits for every four bits of message. This walkthrough encodes a four-bit message, damages one bit, and then finds and repairs the damage using nothing but counting.
The Starting Point
The message is four bits, the 0s and 1s that computers use to store everything:
1 \; 0 \; 1 \; 1
You'll send it using the seven-position code that Richard Hamming described in 1950, often called the (7, 4) Hamming code: 7 bits are sent, and 4 of them carry the message (Hamming 1950, 151–153). The other three are parity bits. A parity bit is an extra bit chosen so that a given group of bits contains an even number of 1s.
The seven positions are numbered 1 through 7. Positions 1, 2, and 4 hold the parity bits. Positions 3, 5, 6, and 7 hold the message (Hamming 1950, 152). Each parity bit watches its own group of positions:
| Check | Positions it covers |
|---|
| Check 1 | 1, 3, 5, 7 |
| Check 2 | 2, 3, 6, 7 |
| Check 4 | 4, 5, 6, 7 |
The groups aren't arbitrary. Write each position number in binary, using the place values 4, 2, and 1. Position 6 is 4 + 2, so it's covered by check 4 and check 2. Position 5 is 4 + 1, so it's covered by checks 4 and 1. Each position is covered by exactly the checks that add up to its number (Hamming 1950, 151–152). That is the design choice that will let the checks name a position.
Somewhere between sender and receiver, one bit will flip.
Walking Through the Encoding and Decoding
Step 1: Place the message bits
Put the four message bits, in order, into positions 3, 5, 6, and 7. Leave positions 1, 2, and 4 empty for now.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|
| Bit | ? | ? | 1 | ? | 0 | 1 | 1 |
Step 2: Set the three parity bits
Fill each parity position so that its check group has an even number of 1s.
- Check 1 covers positions 1, 3, 5, 7. Positions 3, 5, 7 hold 1, 0, 1: two 1s, already even. So position 1 gets 0.
- Check 2 covers positions 2, 3, 6, 7. Positions 3, 6, 7 hold 1, 1, 1: three 1s, which is odd. So position 2 gets 1, making four.
- Check 4 covers positions 4, 5, 6, 7. Positions 5, 6, 7 hold 0, 1, 1: two 1s, already even. So position 4 gets 0.
The word you send, positions 1 through 7, is
0 \; 1 \; 1 \; 0 \; 0 \; 1 \; 1
This seven-bit string is a codeword: one of the 16 allowed strings in the code, one for each possible four-bit message.
Step 3: Flip one bit to simulate an error
During transmission, the bit in position 6 flips from 1 to 0. The receiver gets
0 \; 1 \; 1 \; 0 \; 0 \; 0 \; 1
The receiver doesn't know which bit changed, or whether any did. All it has is these seven bits and the rule that each check group should have an even number of 1s.
Step 4: Recompute the checks and read the failures as a number
The receiver counts the 1s in each check group of the received word. A check passes (written 0) if the count is even and fails (written 1) if it's odd.
- Check 1, positions 1, 3, 5, 7: bits 0, 1, 0, 1. Two 1s, even. Passes: 0.
- Check 2, positions 2, 3, 6, 7: bits 1, 1, 0, 1. Three 1s, odd. Fails: 1.
- Check 4, positions 4, 5, 6, 7: bits 0, 0, 0, 1. One 1, odd. Fails: 1.
Now write the results in the order check 4, check 2, check 1: that gives 1, 1, 0. Read as a binary number, 110 means 4 + 2 + 0 = 6. Hamming called this the checking number (Hamming 1950, 150). It points at position 6, which is the bit that flipped.
Why does it work? Position 6 is covered by checks 4 and 2 and by no other check. Flipping it changes the count in exactly those two groups, from even to odd. A flip in any other single position would fail a different combination of checks, so each single error leaves its own signature. If no bit had flipped, all three checks would pass and the checking number would be 0, meaning no error.
Step 5: Flip the bad bit back and recover the message
Flip position 6 from 0 back to 1. The corrected word is
0 \; 1 \; 1 \; 0 \; 0 \; 1 \; 1
which matches the codeword that was sent. Read the message positions 3, 5, 6, and 7: they hold 1, 0, 1, 1. The message 1011 is recovered, and the receiver never had to ask for it again.
Key Considerations
One error, not two. This code is built to correct a single flipped bit. With two flips, it goes wrong in a specific way. Suppose positions 1 and 2 both flip, so the receiver gets 1010011. Check 1 now fails, check 2 fails, and check 4 passes, giving the checking number 011, which is 3. The receiver flips position 3, which was correct, and ends up with three wrong bits instead of two. Hamming's paper also shows how one more parity bit, covering the whole word, lets a receiver detect that two errors occurred, though not where (Hamming 1950, 153–154).
The common mistake: thinking the extra bits are copies of the data. It's natural to assume that error correction works by sending the data more than once. A Hamming code doesn't repeat anything. Each parity bit summarizes a different overlapping group of positions, and it's the pattern of overlaps that locates an error. Copying would need far more bits. Sending each of the four message bits three times takes 12 bits, while the Hamming code takes 7 and still corrects any single error.
Order matters when you read the checks. The checking number is read with check 4 first and check 1 last, because those are the binary place values 4, 2, and 1. Reading the results in the other order, 011 instead of 110, would point to position 3 and introduce a new error.
What the receiver needs. The receiver doesn't store a list of the 16 codewords. The three checks are the same for every message, so the same few counts work for any word that arrives.
Summary
The four-bit message 1011 was encoded into seven bits, damaged in one position, and repaired by three parity checks whose failures, read in binary, named the damaged position.
| Stage | Bits in positions 1 to 7 | Check 4, check 2, check 1 | Verdict |
|---|
| Sent word | 0 1 1 0 0 1 1 | 0, 0, 0 | Codeword |
| Received word | 0 1 1 0 0 0 1 | 1, 1, 0 | Checking number 6: error in position 6 |
| Corrected word | 0 1 1 0 0 1 1 | 0, 0, 0 | Codeword; message 1011 recovered |
- The parity bits in positions 1, 2, and 4 are set so that each check group has an even number of 1s.
- The failed checks, read as the binary number 110, give the error position 6.
- The corrected word passes all three checks, and positions 3, 5, 6, and 7 return the original message.
References
- Hamming, R. W. 1950. "Error Detecting and Error Correcting Codes." Bell System Technical Journal 29 (2): 147–160.