What is Hamming code?

The best-known error-correcting code that corrects 1-bit errors. With r check bits, a codeword is 2^r − 1 bits long, of which 2^r − 1 − r bits carry data. The Hamming (7, 4) code, with r = 3, adds 3 check bits to 4 bits of data. Each check bit is the parity of an overlapping group of bits, and the result of redoing the checks at the receiver (the syndrome) gives the position of the wrong bit as a binary number. Its minimum Hamming distance is 3, and SECDED, which adds 1 bit of overall parity, can also detect 2-bit errors. It is used, for example, for ECC in server memory.

Japanese name: ハミング符号

Hamming code appears in 5 practice questions across 2 topics, including Information Theory and Coding Basics, Logic circuits.

Field: Information Theory and Coding Basics

How the (7, 4) code works

The bit positions are numbered 1 to 7, and the power-of-2 positions 1, 2 and 4 are the check bits. Check 1 makes the number of 1s even over the positions whose number has a 1 in the ones place when written in binary (1, 3, 5, 7), check 2 over those with a 1 in the twos place (2, 3, 6, 7), and check 4 over those with a 1 in the fours place (4, 5, 6, 7). Only the checks corresponding to the binary digits that are 1 in the number of the wrong position fail, so adding the weights of the failing checks gives the wrong position.

Code rate and length

With r check bits the codeword is 2^r − 1 bits long, and the longer the code, as in (7, 4), (15, 11) and (31, 26), the higher the code rate: 4/7 ≈ 0.57, 11/15 ≈ 0.73 and 26/31 ≈ 0.84. In exchange, one codeword can correct only 1 bit, so the longer the codeword, the more likely it is to receive an error of 2 or more bits.

SECDED and memory

Adding 1 bit of overall parity to a Hamming code raises the minimum Hamming distance to 4, allowing single-error correction (SEC) and double-error detection (DED). 64 bits of data need 8 check bits, which is why server memory is 72 bits wide. Flash memory and other devices that must handle more errors use BCH codes or LDPC codes.

Try it: questions about Hamming code

Which of the following is closest to the code rate of the Hamming (7, 4) code, which adds 3 check bits to 4 bits of data to form a 7-bit codeword?

  1. AAbout 0.43
  2. BAbout 0.57
  3. C1.75
Show answer and explanation

Answer: B. About 0.57

The code rate is k/n, the fraction of the codeword length n taken up by the k data bits. For the Hamming (7, 4) code it is 4/7 ≈ 0.57, so about 57% of the bits sent are data. A lower code rate means more check bits and makes it easier to resist errors, but lowers the efficiency of sending data.

In a Hamming (7, 4) code, the seven bits are numbered 1 to 7 from the left, and bits 1, 2 and 4 are check bits. They are set so that the number of 1s is even in bits 1, 3, 5 and 7 (check 1), in bits 2, 3, 6 and 7 (check 2) and in bits 4, 5, 6 and 7 (check 4). If the received word 0110001 has a single-bit error, which of the following is the wrong bit?

  1. ABit 2
  2. BBit 3
  3. CBit 6
Show answer and explanation

Answer: C. Bit 6

In the received word 0110001, bits 1, 3, 5 and 7 are 0, 1, 0, 1, an even number of 1s (check 1 gives 0); bits 2, 3, 6 and 7 are 1, 1, 0, 1, odd (check 2 gives 1); and bits 4, 5, 6 and 7 are 0, 0, 0, 1, odd (check 4 gives 1). Reading the results of checks 4, 2 and 1 in that order, 110, as a binary number gives 6, so bit 6 is wrong. Flipping bit 6 gives 0110011, which satisfies all three checks.

FAQ

What happens with a Hamming code when a 2-bit error occurs?

In a Hamming code without overall parity, a 2-bit error still makes the syndrome point to some nonzero position, so a correct bit is flipped and the error grows to 3 bits. To prevent this, memories and similar devices use SECDED, which adds overall parity, and report a 2-bit error as an error instead of correcting it.

Are Hamming codes still used today?

Yes. They are simple and fast to compute, so they are widely used where latency must be kept small, such as ECC for memory and the protection of caches and registers. Where many errors must be corrected efficiently, as in communication, more powerful codes such as LDPC codes are used.

Hamming code in the explanations

Questions that mention Hamming code (5)

  1. Which of the following is closest to the code rate of the Hamming (7, 4) code, which adds 3 check bits to 4 bits of data to form a 7-bit codeword?
  2. In a Hamming (7, 4) code, the seven bits are numbered 1 to 7 from the left, and bits 1, 2 and 4 are check bits. They are set so that the number of 1s is even in bits 1, 3, 5 and 7 (check 1), in bits 2, 3, 6 and 7 (check 2) and in bits 4, 5, 6 and 7 (check 4). If the received word 0110001 has a single-bit error, which of the following is the wrong bit?
  3. A SECDED code, which corrects 1-bit errors and detects 2-bit errors, is added to 32 bits of data. The number of check bits r of a Hamming code is the smallest value satisfying 2^r ≥ number of data bits + r + 1, and SECDED adds 1 bit of overall parity to this. Which of the following is the number of check bits needed?
  4. When an even parity bit is added to the 7-bit data 1011001, what is the parity bit?
  5. Five Hamming (7, 4) codewords are lined up and sent in the order of bit 1 of each of the five codewords, then bit 2 of each, and so on (interleaving of depth 5). The receiver restores the original order and then corrects a 1-bit error in each codeword. Which of the following is the maximum length of a burst error, concentrated in consecutive bits, that can always be corrected?

Books on Information Theory and Coding Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Parity bit ECC (error-correcting code) Average code length Binary symmetric channel Forward error correction (FEC) Hamming distance Huffman coding Information entropy Kraft inequality Lossless compression

Back to the glossary