A Hamming code protects data against a single flipped bit by adding a few parity bits. The Hamming code calculator takes the number of data bits and gives the parity bits needed, the total code length, the code rate, the overhead, the length of an extended SECDED code and the largest data size those parity bits could protect.
How the calculation works
- Parity bits needed = the smallest r such that 2^r ≥ m + r + 1, where m is the number of data bits. Each parity bit covers a different subset of positions, and together they point to the position of a single error.
- Total code length = m + r. For example, 4 data bits need 3 parity bits and give the classic (7,4) code.
- Code rate = m ÷ (m + r), and the overhead is r ÷ m.
- Extra overall parity bit (SECDED) = one more bit lets the code detect two errors as well as correct one, which is what most computer memory uses.
Example
For 64 data bits the smallest r with 2^r ≥ 64 + r + 1 is 7, because 2^7 = 128 is at least 72. The code is 71 bits long, with a rate of 64 ÷ 71 = 90.1% and an overhead of 7 ÷ 64 = 10.9%. Adding one overall parity bit gives the 72-bit words, called (72,64), used in ECC memory. With 7 parity bits, the code could protect up to 2^7 − 7 − 1 = 120 data bits.
Why it matters
Error-correcting codes are in memory modules, storage devices, wireless links and QR codes. Counting the parity bits is a standard exercise in computer science and electronics courses, and a quick way to see the cost of reliability in a design.
Good to know
The calculator gives the standard Hamming code that corrects one error. It does not build the code word or compute the parity values. A shortened code, where the block is smaller than the maximum for r, still needs the same number of parity bits. Codes for more errors, such as BCH, need more parity bits.
Related tools
The parity bit calculator covers the simplest error check and the Shannon entropy calculator measures the information that data carries.