Skip to content

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.

What do you want to do?
Parity convention

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)

The codeword, one bit per position
Row1234567
RoleP1P2D1P4D2D3D4
Sent0110011

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 calculations
Parity bitPositions coveredOther bits coveredOnes countedParity bit value
P11, 3, 5, 710120
P22, 3, 6, 711131
P44, 5, 6, 701120

Syndrome table

The syndrome is the position of a single flipped bit, so this table is the whole decoder.

What each syndrome value means
Syndrome (binary)Syndrome (decimal)Meaning
0000No error
0011Error at position 1 (P1)
0102Error at position 2 (P2)
0113Error at position 3 (D1)
1004Error at position 4 (P4)
1015Error at position 5 (D2)
1106Error at position 6 (D3)
1117Error at position 7 (D4)

How to Use the Hamming Code Calculator

  1. 1. Choose a mode. "Encode data bits" builds a codeword; "Check a received word" decodes one you already have.
  2. 2. Enter the bits. Four data bits such as 1011 give the classic (7,4) code. Any length up to 247 bits works, and the calculator picks the smallest number of parity bits.
  3. 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. 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

  1. 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.
  2. P1 covers 3, 5, 7, which hold 1, 0, 1 — two ones, so P1 = 0.
  3. P2 covers 3, 6, 7, which hold 1, 1, 1 — three ones, so P2 = 1.
  4. P4 covers 5, 6, 7, which hold 0, 1, 1 — two ones, so P4 = 0.
  5. 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.

Hamming codes by number of parity bits
Parity bits (r)Codeword length (n)Data bits (k)Code rate (k ÷ n)
3740.571
415110.733
531260.839
663570.905
71271200.945
82552470.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.

Related Tools

More in Data, Markup & Code

See all Data, Markup & Code