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.

Five questions this page answers
Question
1How can check bits say which bit flipped?
2Where do the check bits sit in a code word?
3What is a syndrome?
4How does the decoder tell one flip from two?
5How 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

New words on this page
WordMeaning
ECCError-correcting code. Check bits that let a read repair a flipped bit, not only notice it.
Code wordThe data bits and the check bits of one word together, as they are stored.
PositionThe number of a bit in the code word. Here it is the bit number, counted from 0 on the right.
Hamming codeA 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.
SyndromeThe 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 bitOne more check bit, at position 0 here. It is the parity of all other bits of the code word.
SEC-DEDSingle error correcting, double error detecting: a Hamming code with an overall parity bit.
Which positions each check covers, for 4 data bits
Position76543210
In binary111110101100011010001
Holdsdata 3data 2data 1check 4data 0check 2check 1overall
Check 1 coversyesyesyesyes
Check 2 coversyesyesyesyes
Check 4 coversyesyesyesyes
Overall coversyesyesyesyesyesyesyesitself

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

Three Checks Point at the Flipped BitStep through the picture. Each check covers four positions of the code word. The checks that fail spell the number of the flipped position.
  • Memory
  • Control / FSM
  • Bus
  • Processor
Three Checks Point at the Flipped BitStep through the picture. Each check covers four positions of the code word. The checks that fail spell the number of the flipped position. component Code word positions 7 to 1;position 5 has flipped component Check 1 positions 1, 3, 5, 7 component Check 2 positions 2, 3, 6, 7 component Check 4 positions 4, 5, 6, 7 component Syndrome fails: 4 and 1, so 5 component Overall parity position 0: covers all component Repair flip position 5 back covers 5not 5covers 5all 8 bitsfails: 1passes: 0fails: 4position 5odd: one flip
  1. component Code wordpositions 7 to 1; position 5 has flipped
    • covers 5 → Check 1
    • not 5 → Check 2
    • covers 5 → Check 4
    • all 8 bits → Overall parity
  2. component Check 1positions 1, 3, 5, 7
    • fails: 1 → Syndrome
  3. component Check 2positions 2, 3, 6, 7
    • passes: 0 → Syndrome
  4. component Check 4positions 4, 5, 6, 7
    • fails: 4 → Syndrome
  5. component Syndromefails: 4 and 1, so 5
    • position 5 → Repair
  6. component Overall parityposition 0: covers all
    • odd: one flip → Repair
  7. component Repairflip position 5 back
  1. 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.

  2. 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.

  3. Two checks fail

    At the read, checks 1 and 4 find an odd count. Check 2 finds an even one.

  4. 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.

  5. 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.

Encoding 1011
StepPositions usedSumResult
Place the data7, 6, 5 and 31, 0, 1 and 1
Check bit 13, 5, 71 + 1 + 1 is odd1
Check bit 23, 6, 71 + 0 + 1 is even0
Check bit 45, 6, 71 + 0 + 1 is even0
Overall parity1 to 7four ones: even0

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.

Decoding 1000 1010
CheckBits at its positionsCount of onesFails?
Check 1: positions 1, 3, 5, 71, 1, 0, 13: oddyes
Check 2: positions 2, 3, 6, 70, 1, 0, 12: evenno
Check 4: positions 4, 5, 6, 70, 0, 0, 11: oddyes
Overall: all eight bits3: oddodd

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

  1. Rule 1: Check bits sit at the positions that are powers of two.

    Data fills the other positions, from 3 upward.

  2. 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.

  3. Rule 3: At a read, each check is done again. The numbers of the checks that fail are added.

    That sum is the syndrome.

  4. Rule 4: Syndrome 0 and even overall parity: no error.

    The data is used as it is.

  5. 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.

  6. Rule 6: Even overall parity and a syndrome that is not 0: two errors.

    They cannot be repaired. The read must report them.

  7. 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.

Printed by the lab on Icarus. Code words are shown from position 7 on the left to position 0 on the right.
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=1

Rules 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

Check bits for a word, counted with rule 7
Data bitsCheck bits that name the positionWith the overall parityStored bits
4348
84513
326739
647872

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 choiceThe alternativeWhat the choice buys, and costs
Groups by binary digitGroups picked by handThe syndrome is the position, with no table. The check bits must sit at the powers of two.
An overall parity bitThe Hamming code aloneTwo flips are reported and not "repaired" into a third error. One more bit.
Repair in the read pathReport, and let software repairThe program never sees the error. Every read passes through more gates.
One code word for each memory wordOne code over many wordsA 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:

The same lab, with a decoder that looks at the syndrome alone.
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 0001

The 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 page4 data bits in 8
A word of 64 bits8 check bits: 72 bits stored

This section looks ahead. The same code protects the memories of servers and of cars, at larger widths.

  1. 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.

  2. 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.

  3. Stronger codes

    Flash memory and large DRAM use codes that repair several bits in a block. They are beyond this track.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=1

Check Yourself

Predict the outputThe data 0101 is written into word 3. Position 6 flips, and the word is read. What does the read row say?
// A memory of four words with a Hamming code. Each word of 4 data bits is stored as a code word of 8 bits.
module tb;
  logic [7:0] mem [0:3];              // one code word of 8 bits for each word. Bit p is position p

  function automatic logic [7:0] encode(input logic [3:0] d);
    logic [7:0] c;
    c[3] = d[0];  c[5] = d[1];  c[6] = d[2];  c[7] = d[3];   // data sits where the position is not a power of two
    c[1] = c[3] ^ c[5] ^ c[7];                               // check bit 1: the positions with a 1 in bit 0 of their number
    c[2] = c[3] ^ c[6] ^ c[7];                               // check bit 2: the positions with a 1 in bit 1
    c[4] = c[5] ^ c[6] ^ c[7];                               // check bit 4: the positions with a 1 in bit 2
    c[0] = c[1] ^ c[2] ^ c[3] ^ c[4] ^ c[5] ^ c[6] ^ c[7];   // the overall parity: every other position
    return c;
  endfunction
  function automatic logic [2:0] syndrome(input logic [7:0] c);   // the three checks, done again on what was read
    return {c[4] ^ c[5] ^ c[6] ^ c[7], c[2] ^ c[3] ^ c[6] ^ c[7], c[1] ^ c[3] ^ c[5] ^ c[7]};
  endfunction

  task automatic write(input int a, input logic [3:0] d);
    mem[a] = encode(d);
    $display("write %0d | data %04b | code word %08b", a, d, mem[a]);
  endtask
  task automatic hit(input int a, input int p);          // a particle flips one stored bit
    mem[a] = mem[a] ^ (8'd1 << p);
    $display("hit   %0d | position %0d flips | code word %08b", a, p, mem[a]);
  endtask
  int reads = 0, clean = 0, corrected = 0, detected = 0;

  task automatic read(input int a);
    logic [7:0] c;
    logic [2:0] s;
    logic odd;
    c = mem[a];  s = syndrome(c);  odd = ^c;              // odd: the eight bits hold an odd number of ones
    reads++;
    $write("read  %0d | code word %08b | syndrome %0d | overall parity ", a, c, s);
    if (odd) $write("odd | "); else $write("even | ");
    if (s == 0 && !odd) begin clean++;  $display("no error | data %b%b%b%b", c[7], c[6], c[5], c[3]); end
    else if (odd) begin                                   // an odd number of flips: taken as one
      c = c ^ (8'd1 << s);  corrected++;                  // the syndrome is the position. 0 is the overall parity bit
      $display("one error at position %0d: corrected | data %b%b%b%b", s, c[7], c[6], c[5], c[3]);
    end else begin detected++;  $display("two errors: detected, no data"); end
  endtask
  task automatic totals;
    $display("reads=%0d clean=%0d corrected=%0d detected=%0d", reads, clean, corrected, detected);
  endtask

  initial begin
    write(3, 4'b0101);
    hit(3, 6);
    read(3);
  end
endmodule
ConceptWhere do the check bits of a Hamming code sit?
ConceptChecks 1 and 2 fail, and check 4 holds. Which position does the syndrome name?
ConceptThe syndrome is 6 and the overall parity is even. What has happened?
ConceptHow many check bits does SEC-DED need for a word of 32 bits?
ConceptWhat does the overall parity bit add to a Hamming code?

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.

Practice

Report a problem with this page