Advertisement
Advertisement
DEVELOPER

Hamming Code Calculator — encode, decode and correct one bit

Build a Hamming codeword from your data bits, then feed a corrupted word back in and watch the syndrome point at the flipped bit.

Encoding adds the parity bits. Decoding recomputes them, compares, and reports which single position disagrees.
In encode mode these are the data bits, any length from 1 to 32. In decode mode paste the whole received codeword. Spaces are ignored; anything other than 0 and 1 is rejected with a message.
The extra bit makes the code single-error-correcting and double-error-detecting. Without it, two flips are silently miscorrected into a third error.
Even is the textbook default: each check group sums to zero modulo two. Odd flips every check bit and behaves identically for correction.
Codeword
 
 
0
Data bits carried
0
Parity bits added
0
Total codeword length
0
Code rate, data over total
Tip: the syndrome is not a flag, it is an address. Read the check results as a binary number and you get the one-based position of the bit that flipped, which is the trick that makes Hamming codes worth teaching.
Advertisement

The Hamming code calculator above does two jobs that are really one job seen from opposite ends. Given a run of data bits it inserts parity bits at the power-of-two positions and hands back a complete codeword. Given a received codeword it recomputes those same parity checks, assembles the failures into a syndrome, and tells you either that the word is clean or exactly which single bit has to be flipped to make it clean again.

This is a teaching tool, and Arb Digital would rather say so on the page than let anyone assume otherwise. Nothing here is a production error-correction library. Real storage and transmission systems use Reed-Solomon, BCH, LDPC or turbo codes, chosen against a measured error model and implemented in hardware. What a Hamming code gives you is the clearest possible demonstration of how redundancy buys the ability to repair damage rather than merely notice it, and that idea is worth understanding before any of the harder codes make sense.

What This Hamming Code Calculator Does

Richard Hamming's construction places check bits at positions 1, 2, 4, 8 and so on, and fills the remaining positions with data. Each check bit covers every position whose index has that check bit's power of two set. Check bit 1 covers positions 1, 3, 5, 7, 11 and the rest of the odd numbers. Check bit 2 covers 2, 3, 6, 7, 10, 11. Check bit 4 covers 4, 5, 6, 7, 12 through 15. The overlap is deliberate: every data position sits in a unique combination of check groups, so a failure pattern identifies it uniquely.

The calculator generalises this to any data length up to 32 bits. It finds the smallest number of check bits m satisfying 2mk + m + 1, where k is the number of data bits, then lays out the codeword and computes the checks. Four data bits need three check bits and give the classic Hamming(7,4). Eleven data bits need four and give Hamming(15,11). Twenty-six need five, for Hamming(31,26). The efficiency improves as the block grows, which is why longer blocks are cheaper per data bit and also why they are more likely to suffer two errors in one block.

The linear block code framework this sits inside is standard undergraduate material. The reading list for MIT OpenCourseWare 6.02, Introduction to EECS II: Digital Communication Systems, devotes a chapter to coping with bit errors using error correction codes and another to linear block codes, encoding and syndrome decoding, which is precisely the machinery this page implements.

How to Use It

  1. Pick a mode first. Encode expects data bits only. Decode expects a full codeword including the parity bits, which is a different length, and mixing the two up is the commonest way to get a confusing answer.
  2. Type or paste the bits. Spaces are stripped so you can group them for readability. Any other character produces a written message rather than a wrong answer.
  3. Decide whether you want the extended code. The extra overall parity bit costs one position and buys the ability to distinguish a correctable single error from an uncorrectable double error.
  4. Read the hero line. In encode mode it is the codeword. In decode mode it is the verdict: clean, corrected at a stated position, or a detected double error.
  5. Test the round trip. Encode something, copy the codeword, switch to decode, paste it, and confirm the tool reports no error. Then change one character and confirm it names that exact position.

The Formula: How the Codeword Is Built

Number the codeword positions from one. Position p is a check bit if p is a power of two, otherwise it is the next data bit in order. Check bit at position 2i is the exclusive-or of every data bit whose position index has bit i set. Under even parity the check bit is that exclusive-or directly; under odd parity it is the complement.

Work the default. The data bits are 1011, so d1=1, d2=0, d3=1, d4=1, and they land at positions 3, 5, 6 and 7. Check bit p1 covers positions 1, 3, 5, 7, so it is the exclusive-or of 1, 0 and 1, which is 0. Check bit p2 covers 2, 3, 6, 7, giving 1 ⊕ 1 ⊕ 1 = 1. Check bit p4 covers 4, 5, 6, 7, giving 0 ⊕ 1 ⊕ 1 = 0. Reading positions 1 through 7 in order, the codeword is 0110011.

Now corrupt it. Flip position 5 and the received word is 0110111. Recomputing, the group on positions 1, 3, 5, 7 now sums to 1, the group on 2, 3, 6, 7 sums to 0, and the group on 4, 5, 6, 7 sums to 1. Writing those three results as a binary number with the position-4 check as the most significant digit gives 101, which is five. Flip position five back and you recover 0110011 exactly. That is the whole mechanism, and it is why the check bits sit at powers of two rather than being bolted on at the end.

Advertisement

Where Hamming Codes Are Actually Used, and Where They Stop

The extended Hamming code is not a museum piece. Single-error-correcting, double-error-detecting codes derived directly from Hamming's construction are the standard mechanism in ECC memory, where a single bit flipped by a stray particle or a marginal cell is corrected transparently and a double flip raises a machine check rather than passing bad data to software. The same family appears in some cache and register-file protection and in simple telemetry links.

Where it stops is equally important, and this is the sentence that matters most on this page. A plain Hamming code corrects exactly one error per block and detects exactly two. Give it three flips and it will not merely fail, it will confidently correct the wrong bit and hand you a codeword that is valid, wrong, and indistinguishable from good data. That is worse than no protection at all if you were relying on it, which is why every serious deployment pairs the code with an error model showing that multi-bit events in a block are rare enough to ignore.

It also assumes errors are independent. Burst errors, where a scratch or an interference spike takes out a run of adjacent bits, defeat a Hamming code immediately because they land several failures in one block. The standard answer is interleaving, which spreads the bits of each codeword across the medium so a physical burst becomes one isolated error in each of many blocks, or a code designed for bursts in the first place. Reed-Solomon works over symbols rather than bits for exactly this reason.

Three Cousins, Three Different Jobs

This page sits alongside two others that people routinely confuse, so here are the boundaries in one sentence each. This tool encodes and decodes, adding enough redundancy to locate and repair a flipped bit. The Hamming distance calculator only counts how many positions two equal-length strings differ in, which is the metric this code is designed around but is not itself any kind of encoding. The parity bit calculator adds a single check bit to a word, which detects an odd number of flips and corrects nothing at all.

The relationship between the three is the interesting part. The NIST Dictionary of Algorithms and Data Structures defines Hamming distance as the number of bits which differ between two binary strings, and a code's minimum distance decides what it can do: distance three corrects one error, distance four corrects one and detects two, which is exactly the step from plain to extended Hamming.

Even, Odd, and Why the Choice Barely Matters

Odd parity complements every check bit, so the all-zeros word stops being a valid codeword. That is the one practical argument for it: a link that fails to a constant zero produces a word that odd parity rejects and even parity happily accepts. Beyond that failure mode the two conventions are equivalent, because the syndrome under odd parity is computed against the complemented expectation and lands on the same position. The calculator implements both so you can confirm that for yourself rather than taking it on trust.

A related subtlety is bit ordering. This tool numbers positions from one, left to right, which is the usual textbook convention. Hardware implementations often number from the other end or gather the parity bits into a systematic form. Neither is wrong, but a codeword produced under one convention decodes to nonsense under another, and that mismatch explains most disagreements between a hand calculation and a datasheet.

Reading the Syndrome When Nothing Is Wrong

A zero syndrome means every check group agrees, which people naturally read as "the data is fine". It is worth being precise about what has actually been established. A zero syndrome means the received word is a valid codeword. It does not mean it is the codeword that was sent. Enough errors will move a word from one valid codeword to another, and the decoder has no way to know, because a code with no invalid patterns left to detect has nothing left to say.

This is a property of every error-correcting code without exception. Redundancy buys a radius of protection around each valid word, and outside that radius the guarantee ends. For a purely detection-oriented contrast, the checksum comparator works with cryptographic digests, which are far stronger at detection and offer no correction whatsoever.

Need a website that loads fast and actually works?

Arb Digital builds free tools like this one because useful pages earn attention. If you want tools, calculators or content built for your own audience, we can help.

Browse All Free Tools Talk to Arb Digital

Common Mistakes to Avoid

  • Pasting a codeword into encode mode — it will be treated as fresh data and wrapped in a second layer of parity, which is valid but not what you meant.
  • Assuming a correction is always right — with two or more errors the decoder still produces a confident answer, and that answer is wrong.
  • Numbering positions from zero — the syndrome is a one-based address, so a syndrome of five means the fifth character, not the sixth.
  • Mixing parity conventions between encoder and decoder — every check bit differs, so a word encoded odd and decoded even reports errors that never happened.
  • Expecting protection against burst errors — adjacent flips land in one block and break the single-error assumption the whole scheme rests on.

Related Free Tools From Arb Digital

Measure the property this code is built around with the Hamming distance calculator, and see the minimal case with the parity bit calculator. Turn characters into bits first with the text to binary converter or read them back with the binary to text converter. The bitwise calculator handles the exclusive-or operations by hand, the number base converter moves syndromes between binary and decimal, and the LFSR calculator covers the shift-register arithmetic behind cyclic codes. Everything else lives in the free online tools hub.

Frequently Asked Questions

How many errors can a Hamming code fix?

Exactly one per block. A plain Hamming code corrects a single flipped bit and cannot reliably tell you that two bits flipped. The extended version, with one extra overall parity bit, still corrects only one but does detect that a double error has occurred rather than miscorrecting it.

Why are the check bits at positions 1, 2, 4 and 8?

Because that placement makes the syndrome a binary address. Each check bit covers the positions whose index has its own power of two set, so the pattern of failed checks, read as a binary number, is the position of the faulty bit. Any other placement loses that property.

What is the difference between this and the Hamming distance calculator?

This page encodes and decodes, adding redundancy so a flipped bit can be located and repaired. The distance calculator only counts how many positions two equal-length strings differ in. Distance is the measure that determines what a code can correct, but measuring it is not encoding.

Can I use this for real error correction in production?

No, and it is not intended for that. It is a teaching tool. Production systems use codes chosen against a measured error model and implemented in vetted libraries or hardware, typically Reed-Solomon, BCH or LDPC rather than a bare Hamming code.

What happens if three bits flip?

The decoder computes a syndrome, points at some position, and corrects it. That position is wrong, and the result is a valid codeword carrying wrong data with no indication of a problem. This is a property of the code, not a bug in the tool.

Does even or odd parity change the correction?

No. Odd parity complements every check bit, which stops the all-zeros word being valid and so catches a link that has failed to a constant zero. The syndrome still resolves to the same position, so error correction behaves identically either way.

Why does the code rate improve with longer blocks?

Because the number of check bits grows logarithmically while the data grows linearly. Four data bits need three checks, a rate of 0.571, while twenty-six data bits need only five, a rate of 0.839. The cost is that a longer block is more likely to contain two errors.

Do burst errors break it?

Yes. A burst puts several flips in one block, which violates the single-error assumption immediately. The usual fix is interleaving, which spreads each codeword's bits across the medium so a physical burst becomes one isolated error in each of many blocks.

This tool is provided for education only. It is not an error-correction library and must not be relied on to protect stored or transmitted data. Systems that need real error correction should use a code selected against a measured error model and implemented with vetted, tested software or hardware.

Advertisement
Advertisement

Take it further