ScienceComputer Science

Hamming Code
calculator.

Add parity bits to data so one flipped bit can be found and fixed, or check a word.

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 pp covers every position whose number, written in binary, has the pp 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 rr checks must point to any of the nn positions, or say "no error". That gives the condition below, where kk is the number of data bits.

2r\displaystyle 2^{r}≥k+r+1\displaystyle {}\ge k + r + 1

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 23=82^{3} = 8 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.

Open this example in the tool

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.

Open this example in the tool

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.

Open this example in the tool

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.

Hamming code sizes
Data bitsParity bitsCodeRate
43(7, 4)57%
114(15, 11)73%
265(31, 26)84%
576(63, 57)90%
1207(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.

Questions people ask

Use the smallest number of parity bits r for which 2 to the power r is at least the data bits plus r plus 1. Four data bits need 3, 11 need 4, and 26 need 5. Add one more if you want SECDED.

Sources