Hamming Code Calculator
Enter data bits and get the Hamming codeword with every parity bit worked out, or paste a received word and see which checks fail, what the syndrome says and which bit to flip back. Switch on the overall parity bit to turn the code into SECDED, which also detects double errors. Free, no sign-up, and everything runs in your browser.
Up to 247 bits, leftmost first. Four bits give the classic Hamming (7,4) code; eleven give (15,11). Spaces are ignored.
Positions to corrupt, such as 5 or 2, 6. Leave empty for a clean word. Valid positions run from 1 to 7.
Result
0110011
Codeword
(7, 4)
Code (length, data bits)
0.571
Code rate (data ÷ total)
| Row | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Role | P1 | P2 | D1 | P4 | D2 | D3 | D4 |
| Sent | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
How each parity bit is chosen
Each parity bit sits at a power-of-two position and covers every position whose binary index contains that power. It is set so the covered group has even parity.
| Parity bit | Positions covered | Other bits covered | Ones counted | Parity bit value |
|---|---|---|---|---|
| P1 | 1, 3, 5, 7 | 101 | 2 | 0 |
| P2 | 2, 3, 6, 7 | 111 | 3 | 1 |
| P4 | 4, 5, 6, 7 | 011 | 2 | 0 |
Syndrome table
The syndrome is the position of a single flipped bit, so this table is the whole decoder.
| Syndrome (binary) | Syndrome (decimal) | Meaning |
|---|---|---|
| 000 | 0 | No error |
| 001 | 1 | Error at position 1 (P1) |
| 010 | 2 | Error at position 2 (P2) |
| 011 | 3 | Error at position 3 (D1) |
| 100 | 4 | Error at position 4 (P4) |
| 101 | 5 | Error at position 5 (D2) |
| 110 | 6 | Error at position 6 (D3) |
| 111 | 7 | Error at position 7 (D4) |
How to Use the Hamming Code Calculator
- 1. Choose a mode. "Encode data bits" builds a codeword; "Check a received word" decodes one you already have.
- 2. Enter the bits. Four data bits such as
1011give the classic (7,4) code. Any length up to 247 bits works, and the calculator picks the smallest number of parity bits. - 3. Pick the options. Even parity is the textbook convention; odd parity is the mirror image. The overall parity checkbox adds the extra bit that makes the code SECDED.
- 4. Corrupt it if you like. In encode mode, list positions to flip, such as
5, and the calculator decodes the damaged word and shows how the error is found and repaired.
How a Hamming Code Works
Number the bit positions of the codeword from 1. The positions that are powers of two — 1, 2, 4, 8 — hold parity bits, and every other position holds a data bit. Parity bit P1 covers every position whose binary number has its lowest bit set (1, 3, 5, 7, …), P2 covers those with the next bit set (2, 3, 6, 7, …), P4 those with the third bit set (4, 5, 6, 7, …), and so on. Each parity bit is chosen so its group contains an even number of 1s.
That layout is what makes decoding trivial. Recompute each group on the received word and write the failed checks as a binary number, P1 as the lowest digit. This is the syndrome, and for a single flipped bit it is exactly the position of that bit — because every position belongs to precisely the groups its binary digits name. Syndrome 0 means every check passed.
Worked Example: Encoding 1011
- Place the data. Positions 3, 5, 6 and 7 take the data bits 1, 0, 1, 1. Positions 1, 2 and 4 are left for parity.
- P1 covers 3, 5, 7, which hold 1, 0, 1 — two ones, so P1 = 0.
- P2 covers 3, 6, 7, which hold 1, 1, 1 — three ones, so P2 = 1.
- P4 covers 5, 6, 7, which hold 0, 1, 1 — two ones, so P4 = 0.
- Read the codeword from position 1 to 7:
0110011.
Now flip position 5 in transit, giving 0110111. P1's group (1, 3, 5, 7) now holds three ones and fails; P2's group still holds an even number and passes; P4's group (4, 5, 6, 7) holds three ones and fails. The failed checks, P4 and P1, read as binary 101, which is 5. Flip position 5 back and the word is 0110011 again. The calculator's "Flip bits" box runs exactly this.
Sizes and the Overhead of Correction
With r parity bits there are 2r syndromes. They must name every position and also say "no error", so a code of length n needs 2r ≥ n + 1, which is the same as k + r + 1 ≤ 2r for k data bits. A code that meets the bound with equality is called perfect.
| Parity bits (r) | Codeword length (n) | Data bits (k) | Code rate (k ÷ n) |
|---|---|---|---|
| 3 | 7 | 4 | 0.571 |
| 4 | 15 | 11 | 0.733 |
| 5 | 31 | 26 | 0.839 |
| 6 | 63 | 57 | 0.905 |
| 7 | 127 | 120 | 0.945 |
| 8 | 255 | 247 | 0.969 |
Other data lengths use a shortened code: 8 data bits need 4 parity bits and give a (12, 8) code, where the unused positions simply do not exist. A syndrome that points past the end of a shortened word cannot come from one error, and the calculator reports it as such.
Adding the Overall Parity Bit: SECDED
A plain Hamming code has minimum distance 3: it corrects one error, but two errors look like a different single error and get "fixed" wrongly. Try flipping positions 3 and 5 of 0110011 with the overall bit off — the syndrome is 6, and the decoder confidently flips position 6, returning data 0101 instead of 1011.
Adding one more bit at position 0, set so the whole word has even weight, raises the minimum distance to 4. Now the syndrome and the overall parity together tell the cases apart: a non-zero syndrome with failed overall parity is one correctable error; a non-zero syndrome with passing overall parity is two errors, detected but not correctable; a zero syndrome with failed overall parity is an error in the overall bit itself. That scheme is SECDED — single-error correction, double-error detection. The (72, 64) code used in ECC memory is of this kind: 64 data bits, 7 Hamming parity bits and one overall bit.
Frequently Asked Questions
- What is a Hamming (7,4) code?
- It is a Hamming code with 7-bit codewords carrying 4 data bits and 3 parity bits. Any single flipped bit in the 7 can be located and corrected, and every pair of valid codewords differs in at least 3 positions. There are 16 valid codewords, one per 4-bit data value.
- How do you find the error position in a Hamming code?
- Recompute each parity check on the received word, write 1 for a failed check and 0 for a passed one, and read the results as a binary number with the P1 check as the lowest digit. That number is the position of the flipped bit, counting from 1 at the left. If it is 0, no single-bit error occurred.
- How many parity bits do I need for a given number of data bits?
- The smallest r such that 2r ≥ k + r + 1. Four data bits need 3 parity bits, 11 need 4, 26 need 5, and 64 need 7. The calculator works this out for any length up to 247 data bits.
- What is the difference between even and odd parity?
- With even parity each check group is set to contain an even number of 1s; with odd parity, an odd number. Both detect and locate errors equally well, and the syndrome works the same way. Even parity is the usual textbook choice, and an all-zero word is a valid codeword only under even parity.
- What does SECDED mean?
- Single-error correction, double-error detection. It is a Hamming code plus one overall parity bit. One flipped bit is corrected; two flipped bits are flagged as uncorrectable instead of being silently mis-corrected. ECC memory commonly uses this.
- Can a Hamming code correct two errors?
- No. A plain Hamming code can only correct one, and two errors are usually mistaken for a different single error. With the overall parity bit it can detect two errors, but fixing two needs a stronger code such as BCH or Reed–Solomon.
- Are the parity bits always at positions 1, 2, 4, 8?
- In the standard textbook layout, yes, and that is what makes the syndrome equal to the error position. Other arrangements, such as systematic codes with all data bits first, describe the same code up to reordering the bits, but the syndrome is then looked up in a table rather than read off directly.