How to use the hamming code encoder and decoder
- Choose Encode data bits, or Check a received word if you already have a word to test.
- To encode, type the data bits (1 to 64 zeros and ones). To simulate errors, list positions to flip, counting from 1 at the left.
- Tick the extended code if you want the extra overall parity bit that can detect two errors.
- Read the codeword, the syndrome, the verdict, the corrected word and the data bits, with a table showing every position.
Formula
parity bits sit at positions 1, 2, 4, 8, ...; data bits fill the rest in order
parity bit 2^i makes the 1s at positions whose number has bit i even
syndrome = XOR of the positions of all 1 bits; a nonzero syndrome is the position of a single error
For k data bits the number of parity bits r is the smallest with 2^r at least k + r + 1. The extended code adds one more bit at the end that makes the whole word even.
Four data bits
The data 1011 goes to positions 3, 5, 6 and 7. Parity bit 1 covers positions 1, 3, 5 and 7: 1 + 0 + 1 = even, so it is 0. Parity bit 2 covers 2, 3, 6 and 7: 1 + 1 + 1 gives 1. Parity bit 4 covers 4, 5, 6 and 7: 0 + 1 + 1 is even, so 0. The codeword is 0110011.
If position 5 is flipped in transit, the received word is 0110111. The positions of its 1 bits are 2, 3, 5, 6 and 7, and 2 XOR 3 XOR 5 XOR 6 XOR 7 = 5, so the syndrome is 5. Flipping position 5 back restores 0110011 and the data 1011.
What the code can and cannot do
A plain Hamming code corrects any single flipped bit but cannot tell a double error from a single one, so two errors are silently "corrected" to the wrong word. The extended code adds an overall parity check: a nonzero syndrome with a failing overall check means one error and can be corrected, while a nonzero syndrome with a passing overall check means two errors, which are detected but not corrected. This design is called SECDED.
Memory modules and storage systems use codes in this family. For error detecting checksums, see the CRC Calculator, and to work with raw bits use the Bitwise Calculator.
Valid word lengths
Only certain lengths are complete Hamming words: 3, 5, 6, 7, 9 to 15, 17 to 31 and so on. In check mode the page tells you which lengths are possible if yours is not one of them. A word of a shortened code, such as 5 bits for 2 data bits, uses the same position rules with the unused positions left out.
Assumptions and limits
- Parity is even, positions count from 1 at the left, and the extra bit of the extended code is placed last.
- Data may have 1 to 64 bits; spaces and underscores in the bit strings are ignored.
- Flip positions beyond the length of the word are rejected.
- A pattern that matches no single position, possible in shortened codes, is reported as not fixable.
- The tests flip every kind of position in hundreds of words and compare with a separate parity-check matrix.
Frequently asked questions
Why are parity bits at powers of two?
The position numbers in binary then show which parity bits cover them, so the syndrome reads out the position of the error directly.
What does a syndrome of 0 mean?
Every parity check passes. That means no error, or an error pattern that the code cannot see, such as three flips that happen to form another codeword.
Why add the extended bit?
It lets the decoder separate one error, which it can fix, from two, which it can only report.
Is Hamming (7,4) the only Hamming code?
No. It is the best known case, with 4 data and 3 parity bits. The same rules give (15,11), (31,26) and so on.