Hamming Code
calculator.
Add parity bits to data so one flipped bit can be found and fixed, or check a word.
- Data bits
- 4
- Parity bits
- 3
- Code rate
- 47
- Code length
- 7 bits
4 data bits need 3 parity bits, because 3 checks give 8 answers, enough for "no error" plus one for each of the 7 positions. Any single flipped bit can be found and fixed.
The codeword
Click a bit to flip it7-bit word 0110011. Parity bits sit at positions 1, 2, 4.
Working
- 1Count the parity bits: the smallest r with 2 to the r at least k + r + 12³ = 8 ≥ 4 + 3 + 1
- 2Put the data bits in the positions that are not powers of 2positions 3, 5, 6, 7 ← 1011
- 3p1 covers every position whose number has the 1 bit set3, 5, 7 hold 2 ones, so p1 = 0
- 4p2 covers every position whose number has the 2 bit set3, 6, 7 hold 3 ones, so p2 = 1
- 5p4 covers every position whose number has the 4 bit set5, 6, 7 hold 2 ones, so p4 = 0
- 6The codeword0110011
- 7Check: every parity group of the codeword holds an even number of 1ssyndrome = 0
Every parity check
| Check | Positions | Ones | Result |
|---|---|---|---|
| p1 | 1, 3, 5, 7 | 2 | Even |
| p2 | 2, 3, 6, 7 | 4 | Even |
| p4 | 4, 5, 6, 7 | 2 | Even |
Check a list of words
Results
| Received | Result | Data |
|---|---|---|
| 0110011 | No errorData 1011 | 1011 |
| 0110111 | Bit 5 fixedData 1011 | 1011 |
| 1110011 | Bit 1 fixedData 1011 | 1011 |
| 0111000 | Bit 5 fixedData 1100 | 1100 |
How it's worked out
Number the bits of the codeword from 1. The positions that are powers of 2, which are 1, 2, 4, 8 and so on, hold parity bits. Every other position holds a data bit, in order. Hamming (1950) chose these positions so each parity bit can be set without the others.
Parity bit covers every position whose number, written in binary, has the bit set. In a 7-bit word, p1 covers 1, 3, 5, 7, p2 covers 2, 3, 6, 7 and p4 covers 4, 5, 6, 7. Each parity bit is chosen so its group holds an even number of 1s.
How many parity bits are needed? The checks must point to any of the positions, or say "no error". That gives the condition below, where is the number of data bits.
To check a received word, redo every parity check. Write 1 for each check that fails and 0 for each that passes, with p1 as the rightmost digit. That binary number is the syndrome. If one bit flipped, the syndrome is its position. A syndrome of 0 means every check passed.
Worked examples
Jin encodes 1011 for a networking class
Four data bits need 3 parity bits, since is at least 4 + 3 + 1. The data goes in positions 3, 5, 6 and 7, and the parity bits fill 1, 2 and 4.
p1 covers positions 3, 5 and 7, which hold 1, 0 and 1. That is two 1s, already even, so p1 = 0. p2 covers 3, 6 and 7, which hold 1, 1 and 1. Three 1s is odd, so p2 = 1. p4 covers 5, 6 and 7, which hold 0, 1 and 1, so p4 = 0.
The codeword is 0110011. Every group now holds an even number of 1s: 2 in the p1 group, 4 in the p2 group, 2 in the p4 group.
Hamming's own example: one bit flips in transit
In his 1950 paper, Hamming takes the codeword 0111100 and changes the 1 in position 5 to a 0. The word that arrives is 0111000.
Redo the checks. The p1 group holds one 1, so it fails. The p2 group holds 2 ones, so it passes. The p4 group holds one 1, so it fails. The failed checks are 1 and 4, and 1 + 4 = 5.
So bit 5 is wrong. Flip it back to get 0111100, then read the data bits from positions 3, 5, 6 and 7: 1100.
Ravi sees how SECDED memory treats two flipped bits
SECDED adds one more bit, P, that makes the whole word even. The data 1011 becomes 01100110. The name stands for single error correction, double error detection.
If one bit flips, as in 01110110, the syndrome is 4 and P fails, so the tool fixes bit 4. If two bits flip, as in 00100010, the syndrome is 4 but P passes, because two flips cancel out in the count for the whole word.
That mix, a syndrome that is not zero with P passing, is the sign of two errors. The decoder cannot tell which two bits changed, so the memory reports an error and the data is read again.
Reading your result
Codeword size. Longer blocks spend a smaller share on parity, but each block can still fix only one bit. The rate is the share of bits that carry data.
| Data bits | Parity bits | Code | Rate |
|---|---|---|---|
| 4 | 3 | (7, 4) | 57% |
| 11 | 4 | (15, 11) | 73% |
| 26 | 5 | (31, 26) | 84% |
| 57 | 6 | (63, 57) | 90% |
| 120 | 7 | (127, 120) | 94% |
With SECDED, 64 data bits need 8 check bits, a rate of 89%. The MIT 6.02 notes name the best-known sizes, (7,4) and (15,11), as the codes that correct one error with the fewest parity bits.
The syndrome. A syndrome of 0 means every check passed. A syndrome that points to a position is the bit to flip. If the word is shorter than a full code, the syndrome can point past its end. Flipping bits 3 and 4 of 101101 gives 7, but the word has 6 positions, so at least two bits changed.
Common mistakes
- Counting from 0. Positions start at 1. If you number from 0, the powers of 2 move and every parity group is wrong.
- Putting the parity bits at the end. Some textbooks list the data first and the parity after, which is a different but valid layout. The syndrome only names the position when the parity bits sit at 1, 2, 4, 8 and so on.
- Mixing even and odd parity. This tool, like Hamming's paper, uses even parity. If a course uses odd parity, every parity bit flips.
- Trusting a syndrome of 0. It means no single bit flipped. With a distance of 3, three flipped bits can turn one codeword into another and pass every check.
- Expecting two errors to be fixed. Without P, two flips give a syndrome that points to a third, innocent bit. Fixing two errors needs a different, stronger code.