Hamming Codes: Correct One Bit, Detect Two
Parity can say that a word is wrong. A few more check bits, each watching a different group of positions, can say which bit is wrong. Then the read can repair it.
You Will Be Able To
- Encode a word of 4 data bits into a Hamming code word, by hand.
- Work out a syndrome, and use it with the overall parity to tell no error, one error and two errors apart.
- Count the check bits that a word of a given width needs.
- Lesson
- S3.27
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
The SRAM of a controller holds the only copy of its variables. A parity error there has no good answer. The data cannot be fetched again, and stopping the machine is a failure too. The memory must repair the bit by itself.
Before You Start
You need the last lesson, S3.12: soft errors, the parity bit, and what parity misses.
You need binary numbers: 5 is 101, which is 4 + 1.
| Question | |
|---|---|
| 1 | How can check bits say which bit flipped? |
| 2 | Where do the check bits sit in a code word? |
| 3 | What is a syndrome? |
| 4 | How does the decoder tell one flip from two? |
| 5 | How many check bits does a word of 32 bits need? |
The Idea: Overlapping Parities That Spell a Position
One parity bit covers the whole word, so its failure names no bit. Now take several parity bits, and let each cover a different group of positions. A flipped bit makes some of them fail, and the pattern of failures can name the bit.
Richard Hamming found the neat way to pick the groups. Give every bit of the stored word a number: its position. Write each position in binary. Parity bit 1 covers the positions with a 1 in the lowest binary digit. Parity bit 2 covers those with a 1 in the next digit. Parity bit 4 covers those with a 1 in the digit after that.
Now let position 5 flip. Five is 101 in binary. So the parities 4 and 1 fail, and parity 2 holds. Add the numbers of the failed parities: 4 + 1 = 5. The failures spell the position. This sum is the syndrome.
The parity bits themselves sit at positions 1, 2 and 4: the powers of two. Each of those lies in one group only, its own. The data fills the other positions. The whole stored word is a code word, and the scheme is a Hamming code. Codes that can repair are called error-correcting codes, or ECC.
One weakness is left. Two flips also give a syndrome, and it points at a third, innocent bit. So one more bit is added at position 0: an overall parity bit over everything. One flip makes the overall parity odd. Two flips leave it even. The code is then called SEC-DED: single error correcting, double error detecting.
New Words
| Word | Meaning |
|---|---|
| ECC | Error-correcting code. Check bits that let a read repair a flipped bit, not only notice it. |
| Code word | The data bits and the check bits of one word together, as they are stored. |
| Position | The number of a bit in the code word. Here it is the bit number, counted from 0 on the right. |
| Hamming code | A code whose check bits sit at positions 1, 2, 4, 8 and so on. Each covers the positions with one certain binary digit at 1. |
| Syndrome | The sum of the numbers of the checks that fail at a read. For one flip it is the position of the flipped bit. |
| Overall parity bit | One more check bit, at position 0 here. It is the parity of all other bits of the code word. |
| SEC-DED | Single error correcting, double error detecting: a Hamming code with an overall parity bit. |
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| In binary | 111 | 110 | 101 | 100 | 011 | 010 | 001 | |
| Holds | data 3 | data 2 | data 1 | check 4 | data 0 | check 2 | check 1 | overall |
| Check 1 covers | yes | yes | yes | yes | ||||
| Check 2 covers | yes | yes | yes | yes | ||||
| Check 4 covers | yes | yes | yes | yes | ||||
| Overall covers | yes | yes | yes | yes | yes | yes | yes | itself |
Read a column downward: the checks that cover a position are the binary digits of its number.
One Picture: The Syndrome Points at the Bit
- Memory
- Control / FSM
- Bus
- Processor
- Three parity checks, three different groups
Each check is a parity over four positions. The groups overlap, and no two positions are in the same set of groups.
- Position 5 flips
Five is 101 in binary: 4 + 1. So position 5 is in the groups of check 4 and check 1, and not in the group of check 2.
- Two checks fail
At the read, checks 1 and 4 find an odd count. Check 2 finds an even one.
- The failures add up to the position
The numbers of the checks that fail are added: 4 + 1 = 5. This sum is called the syndrome. It is the number of the flipped position.
- One flip, or two?
One more parity bit covers the whole word. An odd count there means one flip, and the syndrome may be trusted.
Read the diagram as text
- Code word: positions 7 to 1; position 5 has flipped
- Check 1: positions 1, 3, 5, 7
- Check 2: positions 2, 3, 6, 7
- Check 4: positions 4, 5, 6, 7
- Syndrome: fails: 4 and 1, so 5
- Overall parity: position 0: covers all
- Repair: flip position 5 back
- Code word → Check 1: covers 5
- Code word → Check 2: not 5
- Code word → Check 4: covers 5
- Code word → Overall parity: all 8 bits
- Check 1 → Syndrome: fails: 1
- Check 2 → Syndrome: passes: 0
- Check 4 → Syndrome: fails: 4
- Syndrome → Repair: position 5
- Overall parity → Repair: odd: one flip
Parity said that some bit is wrong. Three overlapping parities say which one.
The Smallest Example, Worked by Hand
Encode the data 1011. Data bit 0 is the rightmost digit.
| Step | Positions used | Sum | Result |
|---|---|---|---|
| Place the data | 7, 6, 5 and 3 | 1, 0, 1 and 1 | |
| Check bit 1 | 3, 5, 7 | 1 + 1 + 1 is odd | 1 |
| Check bit 2 | 3, 6, 7 | 1 + 0 + 1 is even | 0 |
| Check bit 4 | 5, 6, 7 | 1 + 0 + 1 is even | 0 |
| Overall parity | 1 to 7 | four ones: even | 0 |
The code word, from position 7 down to 0: 1010 1010.
Now position 5 flips, and the word reads 1000 1010. Do the three checks again, each over its four positions.
| Check | Bits at its positions | Count of ones | Fails? |
|---|---|---|---|
| Check 1: positions 1, 3, 5, 7 | 1, 1, 0, 1 | 3: odd | yes |
| Check 2: positions 2, 3, 6, 7 | 0, 1, 0, 1 | 2: even | no |
| Check 4: positions 4, 5, 6, 7 | 0, 0, 0, 1 | 1: odd | yes |
| Overall: all eight bits | 3: odd | odd |
Syndrome 4 + 1 = 5, and the overall parity is odd. One error, at position 5.
Flip position 5 back, and take the data from positions 7, 6, 5 and 3: 1011. The read gives the right data, although a stored bit is wrong.
If position 2 flips as well, the syndrome is 5 exclusive-OR 2, which is 7. Position 7 is innocent. But the overall parity is even again, and that gives the two flips away.
The Rules
Rule 1: Check bits sit at the positions that are powers of two.
Data fills the other positions, from 3 upward.
Rule 2: The check bit at position 1, 2 or 4 covers every position whose number has that binary digit at 1.
It is set so that its group holds an even number of ones.
Rule 3: At a read, each check is done again. The numbers of the checks that fail are added.
That sum is the syndrome.
Rule 4: Syndrome 0 and even overall parity: no error.
The data is used as it is.
Rule 5: Odd overall parity: one error, at the position that the syndrome names.
Syndrome 0 means the overall parity bit itself. Flip the bit back.
Rule 6: Even overall parity and a syndrome that is not 0: two errors.
They cannot be repaired. The read must report them.
Rule 7: With r check bits, positions 1 to 2 to the power r, less 1, can be named.
So r check bits serve at most that many positions, check bits included.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.5. See "The Hamming Single Error Correcting, Double Error Detecting Code". Check bits sit at the positions that are powers of two. The checks that fail give the position. One more parity bit tells one error from two.
The book numbers positions from 1 on the left. This page numbers them from the right and puts the overall parity at position 0, so that a position is a bit number. That is a choice.
Three or more flips in one word can look like one flip or like none. No rule of this page covers them.
Now Run It
The lab at the end of this page is test code. It stores each word of 4 data bits as a code word of 8 bits. Every equation of the hand tables is one line of the lab. The task hit flips a stored bit.
write 0 | data 1011 | code word 10101010
read 0 | code word 10101010 | syndrome 0 | overall parity even | no error | data 1011
hit 0 | position 5 flips | code word 10001010
read 0 | code word 10001010 | syndrome 5 | overall parity odd | one error at position 5: corrected | data 1011
hit 0 | position 2 flips | code word 10001110
read 0 | code word 10001110 | syndrome 7 | overall parity even | two errors: detected, no data
write 1 | data 0110 | code word 01100110
hit 1 | position 4 flips | code word 01110110
read 1 | code word 01110110 | syndrome 4 | overall parity odd | one error at position 4: corrected | data 0110
write 2 | data 1111 | code word 11111111
hit 2 | position 0 flips | code word 11111110
read 2 | code word 11111110 | syndrome 0 | overall parity odd | one error at position 0: corrected | data 1111
reads=5 clean=1 corrected=3 detected=1Rules 1 and 2 at work: row 1. The data 1011 becomes 1010 1010, as in the hand table.
Rule 5 at work: rows 3 and 4. Position 5 flips. The read finds syndrome 5 with odd overall parity, and gives 1011.
Rule 6 at work: rows 5 and 6. The first flip was never repaired in the memory, and now position 2 flips too. Syndrome 7, overall parity even: two errors, and no data.
Rule 5 for a check bit: rows 8 and 9. Position 4 holds a check bit. Its flip is found and named like any other.
Rule 5 for position 0: rows 11 and 12. Only the overall parity bit has flipped. The syndrome is 0, and the data was never wrong.
Look again at rows 4 and 5. The read repaired the data on its way out, and left the stored word wrong. What to do about that is the next lesson.
Why It Is Built This Way
| Data bits | Check bits that name the position | With the overall parity | Stored bits |
|---|---|---|---|
| 4 | 3 | 4 | 8 |
| 8 | 4 | 5 | 13 |
| 32 | 6 | 7 | 39 |
| 64 | 7 | 8 | 72 |
Six check bits name 63 positions: enough for 32 data bits and themselves. The wider the word, the smaller the share of check bits.
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Groups by binary digit | Groups picked by hand | The syndrome is the position, with no table. The check bits must sit at the powers of two. |
| An overall parity bit | The Hamming code alone | Two flips are reported and not "repaired" into a third error. One more bit. |
| Repair in the read path | Report, and let software repair | The program never sees the error. Every read passes through more gates. |
| One code word for each memory word | One code over many words | A read needs only its own word. Each word pays its own check bits. |
The Trap: The Repair That Made It Worse
A team saves the overall parity bit. Their decoder trusts the syndrome: if it is not 0, the bit at that position is flipped back. Every test with one flip passes. Then a word takes two hits:
write 0 | data 1011 | code word 10101010
hit 0 | position 5 flips | code word 10001010
hit 0 | position 2 flips | code word 10001110
read 0 | code word 10001110 | syndrome 7 | overall parity even | one error at position 7: corrected | data 0001The last row is the failure. Positions 5 and 2 flipped, and the syndrome is 7. The decoder flips position 7, a bit that was right. The word now has three wrong bits.
The read reports "corrected", and hands 0001 to the program. The data written was 1011. This is worse than no code at all: the system believes the data.
Rule 6 is the check. Test every decoder with two flips, and look at what it reports.
At Scale
This section looks ahead. The same code protects the memories of servers and of cars, at larger widths.
Words of 64 bits
Eight check bits protect 64 data bits. That is why many memory modules are 72 bits wide: Patterson and Hennessy, section 5.5.
In the read path
The checks are trees of exclusive-OR gates between the memory and the bus. For a wide word they can cost a pipeline stage. Common practice.
Stronger codes
Flash memory and large DRAM use codes that repair several bits in a block. They are beyond this track.
On the bus
A code can also protect a word on its way between blocks: APB parity protection shows the simplest case.
Try It
Run the lab. Check row 1 against the encoding table.
Write 0100 into word 3, flip position 7, and read. Predict the syndrome first.
Flip two positions of word 3 of your own choice. Which position would the syndrome blame?
write 0 | data 1011 | code word 10101010 read 0 | code word 10101010 | syndrome 0 | overall parity even | no error | data 1011 hit 0 | position 5 flips | code word 10001010 read 0 | code word 10001010 | syndrome 5 | overall parity odd | one error at position 5: corrected | data 1011 hit 0 | position 2 flips | code word 10001110 read 0 | code word 10001110 | syndrome 7 | overall parity even | two errors: detected, no data write 1 | data 0110 | code word 01100110 hit 1 | position 4 flips | code word 01110110 read 1 | code word 01110110 | syndrome 4 | overall parity odd | one error at position 4: corrected | data 0110 write 2 | data 1111 | code word 11111111 hit 2 | position 0 flips | code word 11111110 read 2 | code word 11111110 | syndrome 0 | overall parity odd | one error at position 0: corrected | data 1111 reads=5 clean=1 corrected=3 detected=1Check Yourself
Common Mistakes
Wrong Leave the check bits out of the checks at the read.
A check bit is stored, so it can flip too. Its own check must find that.
Fix Each check covers its whole group, with the check bit in it.
Wrong Repair whenever the syndrome is not 0.
Two flips also give a syndrome, and it names an innocent bit.
Fix Repair only when the overall parity is odd.
Wrong Treat syndrome 0 with odd overall parity as "no error".
The overall parity bit itself has flipped. That is an error, and it should be counted.
Fix Report one error that was corrected, at position 0.
Remember
- Check bits at positions 1, 2, 4, ... each cover the positions with one binary digit at 1.
- The checks that fail at a read add up to the syndrome: the position of a single flipped bit.
- The overall parity tells the cases apart: odd means one flip, even with a syndrome means two.
- 32 data bits need 7 check bits. 64 need 8.