Hamming code
In computer science and telecommunications, Hamming codes are a family of linear error-correcting codes. They can find one or two wrong bits in a message. They can also fix one wrong bit. The simpler parity code cannot fix errors. It can only notice when an odd number of bits are wrong. Hamming codes are not the same as Huffman code.
Hamming codes are perfect codes. This means they use the fewest extra bits possible for their block length while still having a minimum distance of three. Richard W. Hamming invented them in 1950. He wanted a way to fix mistakes made by punched card readers automatically. In his first paper, he shared his general idea and focused on the Hamming(7,4) code, which adds three parity bits to four bits of data.
Mathematical description
In mathematical terms, Hamming codes are a group of binary linear codes. Each code has a block length and a message length that follow a set pattern. Their parity-check matrix is built by listing all the non-zero columns of a certain length. The dual code of a Hamming code is the shortened Hadamard code, also called a Simplex code.
Use in computer memory
Hamming codes add only a small amount of extra information, so they work best when errors are rare. This is the case in computer memory, usually RAM, where bit errors are very rare. Hamming codes are widely used there. Memory that uses this fixing system is called ECC memory. It often uses an extended Hamming code with one extra parity bit. This extra bit helps the computer tell a single wrong bit apart from two wrong bits. These codes are called SECDED, which stands for single-error correcting and double-error detecting.
History
Main article: Parity bit
Main article: Two-out-of-five code
Main article: Triple modular redundancy
Richard Hamming invented Hamming codes. In the late 1940s, he worked at Bell Labs on the Bell Model V, a electromechanical computer that ran on relays and took several seconds for each step. Its input came from punched paper tape. On weekdays, when the machine found an error, it stopped and flashed lights so the operators could fix the problem. At night and on weekends, when no operators were there, it moved on to the next job. Hamming was frustrated that he had to restart his programs from the beginning. Over the next few years, he worked on a way for computers to fix errors. In 1950, he published what is now called Hamming code. It is still used today, including in ECC memory.
Other ways of checking for errors existed before, but none worked as well for the same amount of extra space. Parity adds one bit that shows whether the number of ones in the data is even or odd. It can show that an error happened, but not which bit caused it, so the data must be sent again. A two-out-of-five code uses five bits and can find many errors, but it cannot fix them. The repetition code sends each bit several times so the receiver can pick the most common answer. This can fix errors, but it is very slow and wasteful, especially when more repeats are used. Hamming codes were a better choice, but the older methods helped show what was needed.
Description
Adding extra error-correcting bits to a message can help find mistakes. A seven-bit message has seven possible single-bit errors. Three error-control bits can show that an error happened and also which bit caused it. Hamming studied older coding methods and gave them names. For example, a parity code is called an (8,7) code, because it has eight bits in total, and seven of them hold data. The code rate is found by dividing the number of data bits by the total number of bits.
Hamming also looked at how different code words are from each other. This is now called the Hamming distance. A code with a distance of k can detect k − 1 errors, but it cannot always fix them. Hamming wanted codes that could fix more errors while still using as few extra bits as possible. His key idea was to let the parity bits overlap, so they check each other as well as the data. In his general method, the error-correcting bits are chosen so that the XOR of all the positions holding a 1 is 0. If this check is not 0 when a message arrives, the number shows which bit was changed. The pattern of wrong checks, called the error syndrome, points to the bit with the error.
| Bit position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ... | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Encoded data bits | p1 | p2 | d1 | p4 | d2 | d3 | d4 | p8 | d5 | d6 | d7 | d8 | d9 | d10 | d11 | p16 | d12 | d13 | d14 | d15 | ||
| Parity bit coverage | p1 | |||||||||||||||||||||
| p2 | ||||||||||||||||||||||
| p4 | ||||||||||||||||||||||
| p8 | ||||||||||||||||||||||
| p16 | ||||||||||||||||||||||
| Parity bits | Total bits | Data bits | Name | Rate |
|---|---|---|---|---|
| 2 | 3 | 1 | Hamming(3,1) (Triple repetition code) | 1/3 ≈ 0.333 |
| 3 | 7 | 4 | Hamming(7,4) | 4/7 ≈ 0.571 |
| 4 | 15 | 11 | Hamming(15,11) | 11/15 ≈ 0.733 |
| 5 | 31 | 26 | Hamming(31,26) | 26/31 ≈ 0.839 |
| 6 | 63 | 57 | Hamming(63,57) | 57/63 ≈ 0.905 |
| 7 | 127 | 120 | Hamming(127,120) | 120/127 ≈ 0.945 |
| 8 | 255 | 247 | Hamming(255,247) | 247/255 ≈ 0.969 |
| 9 | 511 | 502 | Hamming(511,502) | 502/511 ≈ 0.982 |
| ... | ||||
| m | n=2m−1 | k=2m−m−1 | Hamming(2m−1,2m−m−1) | 1−m/(2m−1)→1 |
Hamming codes with additional parity (SECDED)
Hamming codes can find and fix one error in a group of bits. However, they cannot always tell a two-bit error apart from a one-bit error, so some two-bit errors can go unnoticed. To solve this, an extra parity bit can be added. This lets the code find and fix a single error, and also find a double error without fixing it. Error correction is a trade-off between being sure to catch three-bit errors and being able to keep working when single-bit errors happen.
This extended Hamming code, known as SECDED (single error correction, double error detection), was popular in computer memory systems starting with the IBM 7030 Stretch in 1961. Server computers today still keep this level of protection, but they no longer use Hamming's method. The (72,64) Hamming code is still popular in some hardware designs, including Xilinx FPGA families.
[7,4] Hamming code
Main article: Hamming(7,4)
In 1950, Hamming created the [7,4] Hamming code. It takes four data bits and adds three parity bits, making seven bits in total. This code can either find and fix one wrong bit, or find one or two wrong bits without fixing them. Adding one more parity bit makes the [8,4] extended Hamming code (see Hamming(7,4)). This version can find and fix one wrong bit. It can also find two wrong bits, but it cannot fix them.
These codes are described using grids of numbers called matrices. They include a generator matrix and a parity-check matrix. To make a codeword, a data message is combined with the generator matrix. For example, the data bits 1011 become 1011010 in the [7,4] code. The matrices can be changed into other forms that work the same way. This is done by swapping columns or replacing a row with a linear combination of rows. To decode the [8,4] code, first check the extra parity bit. If it shows an error, the [7,4] part can find where the single wrong bit is. If it shows no error, the code can tell whether two wrong bits have happened.

Quiz time!
4 questions about Hamming code
How many wrong bits can a basic Hamming code fix?
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on Hamming code, available under CC BY-SA 4.0.
Images from Wikimedia Commons. Tap any image to view credits and license.