Understanding the Hamming code calculator — concept and principle
Hamming codes add a few check bits to data so that a single flipped bit can be found and corrected. This calculator encodes data bits into a Hamming codeword, or checks a received codeword, computes the syndrome and corrects a single-bit error. It is a classic topic in computer science, digital communications and memory design.
How it is calculated
Positions are numbered from 1. Positions 1, 2, 4, 8 … hold check bits; data bits fill the rest in order.
Check bit p covers every position whose binary number includes p, and is set to make that group's count of 1s even.
Number of check bits k: the smallest k with 2^k ≥ m + k + 1 for m data bits.
Checking: XOR together the positions of all 1s; the result is the syndrome. 0 means no error found; otherwise it is the position of a single wrong bit.
The principle
Each position has a binary number, and each check bit watches the positions sharing one binary digit. If one bit flips, exactly the checks that cover its position fail. Reading the failed checks as binary digits spells out the position number, which is why the syndrome points directly to the error. With k check bits there are 2^k possible syndromes, enough to name any of the m + k positions or "no error". A plain Hamming code corrects one error; two errors produce a wrong correction.
Worked example
Default inputs: encode data bits 1011.
- m = 4 data bits; k = 3 because 2³ = 8 ≥ 4 + 3 + 1. Code = (7, 4).
- Data go to positions 3, 5, 6, 7: 1, 0, 1, 1.
- p1 covers 3, 5, 7: 1 ⊕ 0 ⊕ 1 = 0; p2 covers 3, 6, 7: 1 ⊕ 1 ⊕ 1 = 1; p4 covers 5, 6, 7: 0 ⊕ 1 ⊕ 1 = 0.
- Codeword (positions 1–7) = 0110 011; data bits = 1011.
- No check is performed in encode mode, so error position and syndrome show —.
How to read the result
Send or store the codeword. When checking, a syndrome of zero means no single-bit error was found; a non-zero syndrome within the word length gives the bit to flip back. Practical memory systems often add one more overall parity bit (SECDED) so that double errors can be detected rather than miscorrected.
Common mistakes
- Numbering positions from 0 instead of 1.
- Putting data bits in the power-of-two positions.
- Using odd parity in one place and even in another.
- Trusting the correction when two or more bits may have flipped.
Frequently asked questions
What does (7, 4) mean?
7 total bits carrying 4 data bits, with 3 check bits.
How is the error position found?
XOR the positions of all 1 bits; the result is the syndrome, the position of the error.
Can Hamming codes detect two errors?
Not reliably on their own; adding an overall parity bit (SECDED) allows detection.
Where are Hamming codes used?
In memory error correction and teaching, and as a foundation for more advanced codes.
Sources: R. W. Hamming, "Error Detecting and Error Correcting Codes", The Bell System Technical Journal 29(2): 147–160, 1950