Hamming Code Calculator
Hamming(7,4): encode 4 bits, decode 7-bit words, locate and fix single-bit errors.
What it does
Hamming codes, published by Richard Hamming in 1950, were the first practical single-error correcting codes. The classic Hamming(7,4) takes 4 data bits and adds 3 parity bits to make 7, enough to not just detect a flipped bit but name its position.
The layout follows powers of two: positions 1, 2, and 4 hold parity bits P1, P2, P4, while positions 3, 5, 6, and 7 hold the data bits d1 through d4. Each parity bit covers the positions whose index has that bit set: P1 checks 1, 3, 5, 7; P2 checks 2, 3, 6, 7; P4 checks 4, 5, 6, 7.
Decoding re-runs the three checks on the received word. The results, read as a three-bit binary number, are the position of the flipped bit, zero if everything is clean. Flip it back and the data reads out of positions 3, 5, 6, and 7.
One flip is always corrected; two flips can masquerade as a third position and 'correct' the wrong bit. Real links bolt on CRCs, retransmission, or an extra overall parity bit (SECDED) to cover that gap.
How to use
- Pick encode or decode.
- Encoding: enter exactly 4 binary data bits.
- Decoding: enter the 7 received bits.
- Read the codeword first.
- Read the parity bits or syndrome.
- Check the error position and corrected data.
Formula
- d1..d4
- The four data bits at positions 3, 5, 6, 7.
- P1, P2, P4
- Parity bits at positions 1, 2, 4.
- y1..y7
- Received word bits.
- syndrome
- Three check results S1, S2, S4 read as a position.
Parity bits live in seats whose index is a power of two, and each check covers positions whose index has that power set. Reading the syndrome S4 S2 S1 as binary gives the bad position directly, which is the elegance of Hamming's design; zero means no detected error. Two-bit errors can alias into a valid single position, so Hamming(7,4) alone is not a double-error detector.
Worked example
Example: encode 1010, then repair a flipped bit
Data bits 1010; the received word has bit 7 flipped: 1011011.
- P1 = 1^0^0 = 1, P2 = 1^1^0 = 0, P4 = 0^1^0 = 1.
- Codeword: 1 0 1 1 0 1 0 = 1011010.
- Received 1011011: checks return S1 = 1, S2 = 1, S4 = 1.
- Syndrome 111 is position 7; flip that bit back.
- Corrected: 1011010; data at positions 3, 5, 6, 7: 1010.
- Tool shows codeword 1011010, data 1010, syndrome 111, error position 7.
Reference table
| Data | Parity P1 P2 P4 | Codeword |
|---|---|---|
| 0000 | 0 0 0 | 0000000 |
| 0001 | 1 1 1 | 1101001 |
| 0101 | 0 1 0 | 0100101 |
| 1010 | 1 0 1 | 1011010 |
| 1111 | 1 1 1 | 1111111 |
Limits
- Corrects one flipped bit; two flips can look like a different single error.
- Fixed Hamming(7,4): exactly 4 data bits per word; longer messages chain words.
- No burst or erasure handling; real protocols add CRCs and retransmission.
- The decoder assumes at most one error; more than that produces confident nonsense.
Common mistakes
- Reading the syndrome in the wrong bit order; it is S4 S2 S1, not the check order.
- Assuming two-bit errors are safe rather than aliasing.
- Forgetting that positions count from 1, not 0.
- Reading data from seats 1, 2, 4 instead of 3, 5, 6, 7.
- Encoding with even-parity conventions from other texts; this tool uses the classic odd-count XOR checks.
Try it yourself
Quick checks. Answers sit tucked under each question.
Encode 1111?
Parity 1 1 1; codeword 1111111.
Receive 0011010?
Syndrome 001 is position 1; correct to 1011010, data 1010.
Receive 1011010?
Syndrome 000: clean; data 1010.
Questions
How does Hamming(7,4) work?
Four data bits get three parity bits in positions 1, 2, and 4. Each parity covers a distinct pattern of positions, so any single flip produces a syndrome that names its location.
Why is the syndrome the position?
Each check bit covers positions whose index has its bit set; the three results together form the binary index of the bad bit, with 000 meaning clean.
Can it fix two errors?
No. Two flips produce a syndrome matching some third position and the decoder 'fixes' the wrong bit. An overall parity bit turns it into SECDED: correct one, detect two.
Where is Hamming code used today?
It taught a generation of engineers and lives in legacy and light-duty links; modern storage and networks lean on stronger codes like Reed-Solomon, BCH, and LDPC, often combined with CRCs.
How do I extend to longer data?
General Hamming codes use more parity seats: 8 data bits need 4 parity (12,8), and the same power-of-two layout and syndrome logic scales.
Does the tool accept spaces?
Yes, spaces are stripped; entries must be exactly 4 bits to encode or 7 bits to decode.
Sources
- MIT 6.02: Introduction to EECS II, Lecture 3, Error Correction (PDF)
- Georgia Tech ECE 4601: Error Control Coding, Week 14 (PDF)