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.
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
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.
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.
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!
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.
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.
References
- Hamming Codes — How Data Corrects Itself, Spanning Tree
- But what are Hamming codes? The origin of error correction, 3Blue1Brown
- Richard Hamming, The Art of Doing Science and Engineering