Hamming Code Calculator

Encode a string of 0s and 1s with a Hamming code, click codeword bits to inject errors, and follow how the receiver uses the syndrome to locate and correct them. Choose anything from the (7,4) code for 4 data bits up to the 64-bit SECDED code (72 bits) used in ECC memory, and see in a table which positions each parity bit covers.

Inputs

Codeword (click a bit to inject an error)

Parity (power-of-two positions) Data Overall parity (SECDED)

Parity checks (receiver)

ParityPositions coveredXOR result

Decoding result

Syndrome
–
Errors injected
0
Corrected data
–

Positions count from 1; parity bits sit at powers of two (1, 2, 4, 8…) and data bits fill the other positions from the most significant bit, the textbook layout. Real ECC memories may arrange the bits differently.

Parity positions and the bits they cover

In a Hamming code, positions are numbered from 1 and parity bits are placed at powers of two such as 1, 2, 4 and 8. The parity at position p covers every position whose binary number has the p bit set, and it is chosen so that the XOR of those bits is 0. In the (7,4) code, for example, P1 covers positions 1, 3, 5 and 7, P2 covers 2, 3, 6 and 7, and P4 covers 4, 5, 6 and 7. Encoding the data 1011 gives 0110011 in positions 1 to 7. Use the "Positions covered" column to see which bits each parity checks.

Why the syndrome points to the error

The receiver recomputes the XOR of the positions each parity covers. With no error every check is 0, but if the bit at position i flips, only the checks for the parity bits that are 1 in the binary form of i become 1. Read together, the check results (the syndrome) spell out the error position i. In the (7,4) code, for example, flipping position 5 makes the P1 and P4 checks 1, so the syndrome is 101 in binary, which is 5. Click one codeword bit in the tool and confirm that the syndrome matches the position you clicked and the corrected data returns to the original.

How many parity bits are needed

r parity bits give 2ʳ syndromes, one of which means "no error", so they can distinguish 2ʳ − 1 error positions. With k data bits, the code has k + r positions to tell apart, so the smallest r satisfying 2ʳ ≥ k + r + 1 is needed. Four data bits need 3 parity bits, 8 need 4, and 64 need 7. Longer data means a smaller share of parity bits: the code rate rises from about 57% for (7,4) to about 90% for 64 data bits.

SECDED: telling double errors apart

A plain Hamming code corrects single-bit errors, but when two bits flip, the syndrome points to an unrelated position and the decoder corrects the wrong bit (a miscorrection). Turn SECDED off in the tool and click two bits to see the corrected data differ from the original. SECDED (single error correction, double error detection) adds one overall parity bit. If the overall parity fails and the syndrome is nonzero, it is a single error and gets corrected; if the overall parity holds but the syndrome is nonzero, it is a double error, which is reported without correction. 64 data bits plus 7 + 1 check bits make the 72-bit word long used in server ECC memory.

Where it is used and its limits

Hamming SECDED codes protect server main memory, processor caches and register files, and SRAM against soft errors caused by cosmic rays and radiation. They need little computation and can be encoded and decoded quickly with XOR trees. They cannot handle three or more errors or the failure of many bits in one chip, though, so memories also use schemes that tolerate a whole chip failing, and error-prone media such as flash memory use stronger codes like BCH or LDPC. Where detection alone is enough, as in communication, CRCs are common.

Practice computer arithmetic questions

What does an error-correcting code (ECC) used in memory do?

  1. ACompresses data to increase capacity
  2. BAdds redundant bits to data to detect and correct bit errors
  3. CEncrypts data
Show answer and explanation

Answer: B. Adds redundant bits to data to detect and correct bit errors

ECC adds check bits to data so errors can be detected and corrected on readout. Up to DDR4, server memory typically adds 8 bits to every 64, enabling single-error correction and double-error detection (SECDED); DDR5 modules add 8 bits to each 32-bit subchannel. High-reliability servers go further with schemes such as Chipkill, which survives the failure of an entire chip. DDR5 also builds ECC into each DRAM die.

When an even parity bit is added to the 7-bit data 1011001, what is the parity bit?

  1. A1
  2. B0
  3. CAlways 1 regardless of the data
Show answer and explanation

Answer: B. 0

Even parity sets the parity bit so the total number of 1s, including it, is even. 1011001 already has four 1s, so the parity bit is 0. If the receiver counts an odd number of 1s, a single-bit error is detected, but two simultaneous errors go unnoticed and the faulty bit cannot be located. Memories that need correction use ECC such as Hamming codes, which combine multiple parity bits.

FAQ

What is the (7,4) Hamming code?

It is the smallest practical Hamming code: 3 parity bits are added to 4 data bits to make 7 bits. A 3-bit syndrome can distinguish all 7 positions, so any single-bit error can be corrected. Set the data bits to 4 in the tool to try it.

What happens with a double-bit error?

In a plain Hamming code, the syndrome points to another position, so the decoder flips a correct bit and miscorrects. SECDED adds an overall parity bit, so it recognizes double errors and reports them without correcting.

How does a parity bit differ from a Hamming code?

A single parity bit tells you that an odd number of bits flipped, but not which bit. A Hamming code combines several parity bits covering slightly different positions, so it can locate the error and correct it.

How does a Hamming code differ from a CRC?

A Hamming code is designed to locate and correct errors and is used mainly in memory. A CRC is designed to detect errors with high probability; it does not correct them, and communication links retransmit when it fails. Both are linear codes built on XOR.

What is the bit at position 0?

It is the overall parity bit (P0) added for SECDED, chosen so that the XOR of all bits is 0. If this bit itself flips, the syndrome stays 0 while the overall parity fails, so it is corrected as an error in P0.

Related terms

ECC (error-correcting code) Soft error CRC (cyclic redundancy check)

Related calculators

Semiconductor, Electronics and Computing Calculators →