←

Hamming Codes

10/2026

During a Belgian parliamentary election in 2003, an electronic voting system gave a candidate 4,096 extra votes. Investigators noticed that this discrepancy of 4,096 is exactly 212, evidence of a flipped bit.

This error was unable to be reproduced, leading investigators to believe that the flip was caused by a cosmic particle hitting a bit in the computer’s RAM.

Data stored or sent in any medium is vulnerable to error. And as you can tell, even a single flipped bit can have serious implications.


In 1947, Richard Hamming, a mathematician at Bell Labs, often encountered such errors when using early computers. Frustrated, he devised a clever system that became known as Hamming codes. By adding a few carefully placed bits when creating a message, a sender allows any recipient to detect and correct an error, without knowing the message beforehand.

Below you can see how a 255 bit message can hide a single bit of error. The secret to finding it is lies in the green redudancy bits.

Hover to flip one bit among 255. Eight redundancy bits, placed at the powers of two, are enough to identify it.

Does designing such a solution seem impossible? By the end of this, it will seem quite straightforward.


At the time, there were a variety of methods to check if a message contained an error, this is error detection.

The error detection method Hamming built on was the parity check. In a parity check, the total number of 1s in the message gets counted. If the total contains an odd number of 1s, then an additional parity bit is set to 1. If the message contains an even number of 1s, then the parity bit gets set to 0. This guarantees the total number of 1s is always even.

In order to detect the presence of an error, the recipient simply needs to count the number of 1s.

Try for yourself

10no error detected
Flip a bit, then press Check. The green parity bit keeps the number of 1s even.

Hamming knew that if the error could be detected, then with a bit more information the error could be located. If an error could be located, then simply flipping the bit would correct it.

Adding a parity check to only one half of the message would let the recipient localize the error to a given location.

4no error detected
Flip a bit, then press Check. Only the second half is counted, so an error in the first half stays hidden.

Notice that the parity bit is placed at position 8. Since 8 is 1000 in binary, it checks every position whose fourth binary digit is 1 (positions 8 through 15).

This reveals a useful pattern: by placing parity bits at powers of two, each check c orresponds to a binary digit of the position number. P1 checks the first digit (from the right), P2 the second, P4 the third, and P8 the fourth.

P1
0001
6no error detected
P2
0010
6no error detected
P4
0100
4no error detected
P8
1000
6no error detected
Flip a bit, then press Check. Each parity bit counts the positions whose index has that binary digit set.

Individually, each parity check only tells us whether an error occurred in its group. But combine all four checks in one message, and we can pinpoint the exact position of a flipped bit!

P1P2P4P8
no error located
Flip a bit, then press Check. Each parity check pulses its region; red stays on the remaining suspects until a later check rules them out.

Actually, I lied. There is a blindspot in these codes. If you didn’t already, go back and see if you can find it.

This blindspot is position 0. Its binary representation, 0000, has no 1s, so none of the four parity checks cover it. An error at position 0 would go completely undetected. The solution to this is, simply, to get rid of the 0th bit. This gives what is called a (15, 11) Hamming code: as there are 11 message bits out of 15 total bits.

There’s one last bit of cleverness here. Add up the positions of the failed parity checks (1, 2, 4, or 8), and you get the exact index of the flipped bit. Since we’ve eliminated position 0, a result of 0000 can safely mean no error was detected.

P1P2P4P8
no error located
Flip a bit, then press Check. The syndrome starts at 0000 and updates with each failing check; the final value is the error’s index.

Hamming codes scale incredibly well. Since the parity bits are placed in positions that are powers of 2, for every message that is 2n - 1 bits, only n parity bits are needed.

I'll leave you with a demonstration of how it looks for 255 bits.

P1P2P4P8P16P32P64P128
no error located
Flip a bit among 255, then press Check. Eight parity checks build an 8-bit syndrome — the error’s index.

References