Hamming Code Calculator
Use this Hamming code calculator to encode raw binary data into a Hamming codeword with automatically placed parity bits, or paste a received codeword to detect and fix single-bit transmission errors. Select your mode, enter your bits, and the tool shows the syndrome, error position, corrected codeword, and a step-by-step breakdown of the parity calculations.
What is a Hamming code?
A Hamming code is a linear error-correcting code developed by Richard Hamming at Bell Labs in 1950. It adds a small number of redundancy bits (parity bits) to a block of data bits so that the receiver can detect and correct any single-bit error that occurs during transmission or storage. The key insight is that parity bits are placed at positions that are powers of 2 (1, 2, 4, 8, 16, ...), and each parity bit covers a specific, overlapping subset of the other bit positions. When any single bit flips, the pattern of failed parity checks points directly to the error position, making correction a matter of flipping that one bit back.
How encoding works - parity bit placement
To encode k data bits, first calculate the number of parity bits r needed by finding the smallest r such that 2^r is at least r + k + 1. The total codeword length is n = k + r. Parity bits are placed at positions 1, 2, 4, 8, ... (powers of 2) in the 1-indexed codeword. Data bits fill the remaining positions in order. Each parity bit at position 2^p is responsible for all positions whose binary representation has a 1 in bit p. After placing the data bits, each parity bit is set to make the XOR (exclusive OR) of all the bits it covers equal to zero (even parity). The result is a codeword where any single flipped bit can be uniquely identified by combining which parity checks fail.
How error detection and correction works - the syndrome
At the receiving end, the same parity checks are recomputed over the received codeword. The result is a binary number called the syndrome. If all parity checks pass, the syndrome is zero and the data is assumed correct. If some checks fail, the syndrome equals the decimal index of the failing checks added together - and that number is exactly the 1-indexed position of the single flipped bit. The receiver flips that bit and the corrected codeword is recovered. Data bits are then extracted by reading the non-parity positions. The Hamming code is called SEC (Single Error Correcting). A SECDED extension adds an overall parity bit to also detect (but not correct) double-bit errors.
Code rate and efficiency
The code rate k/n measures efficiency: the fraction of each codeword that carries actual data. Small Hamming codes are relatively inefficient because the parity overhead is proportionally large. Hamming(7,4) has a rate of about 57%, while Hamming(255,247) reaches nearly 97%. As the block size grows, the r = ceil(log2(n+1)) parity bits become a tiny fraction of the total. For modern high-speed storage systems (DRAM ECC, SSD controllers, RAID), extended Hamming or BCH codes are used that can correct multiple errors, but the single-error-correcting Hamming scheme remains the foundation for understanding all block error-correction codes.
Standard Hamming code sizes
| Code | Data bits (k) | Parity bits (r) | Codeword bits (n) | Code rate |
|---|---|---|---|---|
| Hamming(7,4) | 4 | 3 | 7 | 57.1% |
| Hamming(15,11) | 11 | 4 | 15 | 73.3% |
| Hamming(31,26) | 26 | 5 | 31 | 83.9% |
| Hamming(63,57) | 57 | 6 | 63 | 90.5% |
| Hamming(127,120) | 120 | 7 | 127 | 94.5% |
| Hamming(255,247) | 247 | 8 | 255 | 96.9% |
SEC (Single Error Correcting) Hamming codes. n = codeword length, k = data bits, r = parity bits. Code rate = k/n.
Frequently asked questions
Where are the parity bits placed in a Hamming codeword?
Parity bits occupy positions that are exact powers of 2 in the 1-indexed codeword: positions 1, 2, 4, 8, 16, and so on. All other positions carry data bits. For example, in a Hamming(7,4) code, positions 1, 2, and 4 are parity bits and positions 3, 5, 6, and 7 are the four data bits.
Can a Hamming code correct more than one error?
A standard Hamming code (SEC) can only correct one bit error per block. If two bits flip, the syndrome still points to a position but it will be the wrong one, so the decoder mis-corrects and produces bad data. An extended Hamming code (SECDED) adds one overall parity bit, which allows it to detect double-bit errors (and report uncorrectable), though it still cannot fix them. For higher error correction capability, BCH or Reed-Solomon codes are used instead.
What does the syndrome value mean?
The syndrome is the decimal number formed by summing the positions of all failing parity checks. A syndrome of 0 means no error was found. A non-zero syndrome directly gives the 1-indexed position of the flipped bit in the received codeword. For instance, a syndrome of 5 (binary 101) means parity checks at positions 1 and 4 both failed, pointing to bit 5 as the error.
How many parity bits are needed for n data bits?
Find the smallest integer r that satisfies 2^r >= r + k + 1, where k is the number of data bits. For 4 data bits, 3 parity bits are needed (2^3 = 8 >= 3 + 4 + 1 = 8). For 11 data bits, 4 parity bits suffice (2^4 = 16 >= 4 + 11 + 1 = 16). Each additional parity bit roughly doubles the maximum data-bit capacity.
What is the difference between even parity and odd parity Hamming codes?
Most Hamming implementations use even parity: each parity bit is set so the XOR of all bits it covers equals 0. Odd parity sets each parity bit so the XOR equals 1. Both work equally well for error correction; the choice affects only the specific bit values in the parity positions, not the underlying mathematics. This calculator uses even parity, which is the most common convention in textbooks and hardware implementations.
How is a Hamming code different from a CRC?
A Hamming code corrects errors: when a bit flips, the receiver can automatically fix it without re-requesting data. A CRC (Cyclic Redundancy Check) detects errors with high probability but cannot correct them - it signals the receiver to request a retransmission. Hamming codes are used in memory (ECC RAM) where you cannot re-request a read; CRCs are used in networks and storage controllers where the cost of retransmission is acceptable and multi-bit errors need detection.