Safekipedia

Hamming code

Discoverer · ages 10+Adapted from Wikipedia
Graphical depiction of the four data bits and three parity bits and which parity bits apply to which data bits

In computer science and telecommunications, Hamming codes are a family of linear error-correcting codes. They can detect one-bit and two-bit errors, or correct one-bit errors without detecting errors they fail to fix. The simpler parity code cannot correct errors and can detect only an odd number of bits in error. Hamming codes are not the same as Huffman code.

Hamming codes are perfect codes, which means they reach the highest possible rate for codes with their block length and a minimum distance of three. Richard W. Hamming invented them in 1950 as a way to automatically correct errors introduced by punched card readers. In his original paper, he explained his general idea and focused on the Hamming(7,4) code, which adds three parity bits to four bits of data.

In mathematical terms, Hamming codes are a class of binary linear codes. Each code has a block length and a message length that follow a set pattern, and the rate of these codes is the highest possible for codes with a minimum distance of three. 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 known as a Simplex code.

Because Hamming codes add only a limited amount of extra information, they work best when errors are rare. This is the case in computer memory, usually RAM, where bit errors are extremely rare and Hamming codes are widely used. Memory that uses this correction system is known as ECC memory. There, an extended Hamming code with one extra parity bit is often used. It lets the decoder tell a single one-bit error apart from two-bit errors, so these codes are called SECDED, short 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, the inventor of Hamming codes, worked at Bell Labs in the late 1940s on the Bell Model V, an electromechanical computer that ran on relays and took seconds for each step. Its input came from punched paper tape. On weekdays, when the machine found errors, it stopped and flashed lights so operators could fix the problem. At night and on weekends, with no operators present, it moved on to the next job. Hamming grew frustrated having to restart his programs from the beginning. Over the next few years, he worked on a way for computers to fix errors, and in 1950 he published what is now called Hamming code. It is still used today, including in ECC memory.

Earlier error-checking methods existed, but none were as effective for the same amount of extra space. Parity adds one bit showing whether the number of ones in the data is even or odd. It can detect an error, but not which bit caused it, so the data must be sent again. A two-out-of-five code uses five bits and can detect 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. In a seven-bit message there are seven possible single-bit errors, so three error-control bits could show not only that an error happened, but also which bit caused it. Hamming studied older coding methods and gave them names, such as calling a parity code an (8,7) code, because it has eight bits in total, with seven of them holding data. The code rate is found by dividing the number of data bits by the total number of bits.

Hamming also looked at how far apart code words are, which is now called the Hamming distance. A code with a distance of k can detect k − 1 errors but 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 that 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 that check is not 0 when a message is received, the number shows which bit was damaged. The pattern of wrong checks, called the error syndrome, points to the bit in error.

Bit position1234567891011121314151617181920...
Encoded data bitsp1p2d1p4d2d3d4p8d5d6d7d8d9d10d11p16d12d13d14d15
Parity
bit
coverage
p1YesYesYesYesYesYesYesYesYesYes
p2YesYesYesYesYesYesYesYesYesYes
p4YesYesYesYesYesYesYesYesYes
p8YesYesYesYesYesYesYesYes
p16YesYesYesYesYes
Parity bitsTotal bitsData bitsNameRate
231Hamming(3,1)
(Triple repetition code)
1/3 ≈ 0.333
374Hamming(7,4)4/7 ≈ 0.571
41511Hamming(15,11)11/15 ≈ 0.733
53126Hamming(31,26)26/31 ≈ 0.839
66357Hamming(63,57)57/63 ≈ 0.905
7127120Hamming(127,120)120/127 ≈ 0.945
8255247Hamming(255,247)247/255 ≈ 0.969
9511502Hamming(511,502)502/511 ≈ 0.982
...
mn=2m−1k=2m−m−1Hamming(2m−1,2m−m−1)1−m/(2m−1)→1

Hamming codes with additional parity (SECDED)

Hamming codes can detect and fix one error in a group of bits. However, they cannot always tell a double-bit error apart from a single-bit error, so some double-bit errors go unnoticed. To fix this, an extra parity bit can be added. This lets the code detect and fix a single error, and also detect, but not fix, a double error. Error correction is a trade-off between being certain 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 in the 21st century 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)

Graphical depiction of the four data bits and three parity bits and which parity bits apply to which data bits

In 1950, Hamming introduced the [7,4] Hamming code. It turns four data bits into seven bits by adding three parity bits. It can either detect and correct single-bit errors, or detect single and double-bit errors without correcting them. Adding one more parity bit creates the [8,4] extended Hamming code (see Hamming(7,4)). This version can detect and correct single-bit errors and detect, but not correct, double-bit errors.

The codes are described using grids of numbers called matrices, including a generator matrix and a parity-check matrix. A data message is combined with the generator matrix to make a codeword. For example, the data bits 1011 are encoded as 1011010 in the [7,4] code. The matrices can also be rewritten in equivalent forms 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] code can find where the single error is. If it does not show an error, the code can tell whether a double-bit error has happened.

Quiz time!

5 questions about Hamming code

Question 1 of 5

Who invented Hamming codes, and what problem were they first used to solve?

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.