Hamming Code Calculator

Encode data bits into a Hamming codeword, then simulate a single-bit transmission error and watch the syndrome locate and correct it automatically.

Quick Facts

Parity bit count
Smallest r where 2^r ≥ m + r + 1
For 4 data bits, r = 3 and the codeword is 7 bits long — the classic Hamming(7,4) code.
Syndrome rule
XOR of every bit position holding a 1
Zero means a valid codeword; any other value names the exact flipped position.

Your Results

Calculated
Encoded codeword
-
Data + parity bits, position 1 to n
Code parameters
-
Parity bits (r) and total length (n)
Received & syndrome
-
XOR of one-bit positions
Decoded data bits
-
Extracted after correction

Ready

Enter data bits and an optional bit position to flip, then press Calculate.

About the Hamming Code

A Hamming code is a linear error-correcting code that adds carefully placed redundant parity bits to a block of data bits. The result — the codeword — can survive a single bit being flipped (by noise, a storage fault, or a transmission glitch) and still be read correctly, because the pattern of parity checks pinpoints exactly which bit changed. Hamming codes are the basis of ECC (error-correcting code) memory, and a simple teaching example for the broader field of coding theory. This calculator encodes your data bits, optionally flips one bit to simulate an error, and then decodes and corrects it using the standard syndrome method.

The formulas

For m data bits, the calculator finds the smallest number of parity bits r that satisfies:

  • 2r ≥ m + r + 1 — this guarantees enough distinct non-zero syndrome values to point at every one of the m + r bit positions (plus the all-zero "no error" case).
  • Codeword length: n = m + r — the parity bits are inserted at positions that are powers of two (1, 2, 4, 8, ...); the data bits fill every remaining position, left to right.
  • Each parity bit is set using even parity: it equals the XOR of every bit (in its group) whose position, written in binary, has that power-of-two bit set. For example, the parity bit at position 1 covers positions 1, 3, 5, 7, 9, ...; the one at position 2 covers 2, 3, 6, 7, 10, 11, ...
  • Syndrome (decoding): XOR together the position numbers of every bit in the received codeword that equals 1. A valid codeword always produces a syndrome of 0. If exactly one bit was flipped, the syndrome equals that bit's position exactly — flip it back and the codeword, and the original data bits, are recovered.

How to get the best results

  • Enter data bits as a plain string of 0s and 1s only, such as 1011 — no spaces or other characters.
  • Leave Bit position to flip at 0 first to see the clean, error-free codeword and confirm the syndrome comes out to 0.
  • Then set it to any position from 1 to n (shown in the "Code parameters" result) to simulate a transmission error and watch the calculator find and fix it.
  • The classic textbook example is 4 data bits, which always produces the well-known Hamming(7,4) code — 3 parity bits and a 7-bit codeword.

Practical context

Real hardware and protocols use this same mechanism at much larger scale — ECC RAM, satellite and deep-space links, and some flash storage controllers all rely on Hamming-family codes to correct occasional single-bit flips without retransmitting data. A plain Hamming code corrects exactly one bit error per codeword; it cannot reliably fix (and can even miscorrect) two simultaneous errors. Systems that need to catch a second error typically add one more overall parity bit — an "extended" Hamming code, often called SECDED (single error correction, double error detection).

Frequently Asked Questions

What is a Hamming code?
A Hamming code is an error-correcting code that adds redundant parity bits to a block of data bits so a single-bit error introduced during storage or transmission can be automatically located and fixed. The parity bits sit at positions that are powers of two (1, 2, 4, 8, ...), and each one checks a specific overlapping subset of the other bits using even parity.
How many parity bits does m data bits need?
The number of parity bits r is the smallest integer satisfying 2r ≥ m + r + 1, where m is the number of data bits. For 4 data bits, r = 3 and the total codeword length is n = m + r = 7 bits — the classic Hamming(7,4) code.
How does the calculator find and correct a bit error?
It computes the syndrome by XOR-ing together the position numbers of every bit that equals 1 in the received codeword. For a valid codeword the syndrome is 0. If exactly one bit was flipped, the syndrome equals the exact position of that bit, so flipping it back corrects the error before the original data bits are extracted.
Can a Hamming code correct more than one bit error?
No. A standard Hamming code guarantees correction of exactly one bit error per codeword. If two bits flip at once, the syndrome points to the wrong position and the code silently miscorrects. Detecting (not correcting) a second simultaneous error requires an extended Hamming code with one additional overall parity bit, often called SECDED.