Hamming Distance Calculator - Count Differing Positions

Hamming distance calculator that counts the positions where two equal-length binary, decimal, hex, or text strings disagree and reports the similarity percentage.

Updated: June 19, 2026 • Free Tool

Hamming Distance Calculator

Binary accepts only 0 and 1. Decimal accepts digits 0 to 9. Hex accepts digits 0 to 9 plus A to F (upper or lower case). Text accepts printable ASCII characters.

First message. Spaces and a leading 0b / 0x marker are stripped. The cleaned length sets the comparison length after the second message is padded.

Second message. Spaces and a leading 0b / 0x marker are stripped. If the cleaned length is shorter than message 1, the calculator pads it with leading zeros.

Results

Hamming Distance
0
Matching Positions 0
Compared Length 0
Similarity 0%
Difference Positions 0
Status 0

What Is the Hamming Distance Calculator?

A Hamming distance calculator counts the number of positions where two equal-length strings disagree and reports the similarity percentage, the matching-position count, and a list of every differing index.

  • Measuring error drift in a noisy channel: Compare a received bit string against the sent bit string and read off how many bits flipped in transit, without doing the bitwise XOR by hand.
  • Comparing hex fingerprints or version tokens: Paste two equal-length hex tokens and see whether a single hex digit changed, the only signal of a partial upgrade or corrupted config.
  • Checking text similarity with a fixed-length metric: Compare two same-length words or fixed-width IDs and read the matching percentage, useful for typo-tolerant matching when length is known up front.
  • Teaching the metric behind Hamming codes: Walk through the same numbers used in Wikipedia's Hamming code article, where the minimum-distance property of the code is measured with this metric.

The metric was introduced by Richard Wesley Hamming at Bell Labs in 1950 while he was working on error-detecting and error-correcting codes for telephone equipment. It became the natural yardstick for any code whose strength is measured by how many bit flips it can survive, and for binary strings the Hamming distance equals the population count of the bitwise XOR of the two strings.

The minimum-distance property of a Hamming code is measured with the same metric this calculator exposes, so the Hamming Code Calculator is the natural next step when a distance number has to be turned into an actual encoding, decoding, and single-bit correction workflow.

How the Hamming Distance Calculator Works

The calculator cleans both inputs, pads the shorter one with leading zeros, walks the two strings in lockstep, and increments the distance counter at every position where the two characters disagree.

d(x, y) = |{ i : x_i != y_i }| = popcount(x XOR y) (strings x and y must have equal length n)
  • numeralSystem: Alphabet selector. Binary accepts 0 and 1; decimal accepts 0 to 9; hex accepts 0 to 9 plus A to F; text accepts printable ASCII.
  • message1: First string. Spaces and a leading 0b / 0x marker are stripped. Sets the comparison length after message2 is padded.
  • message2: Second string. Spaces and a leading 0b / 0x marker are stripped. If the cleaned length is shorter than message1, it is padded with leading zeros.
  • distance: Integer count of positions where the two padded strings differ.
  • matchingPositions: Integer count of positions where the two padded strings agree.
  • similarity: Percentage of positions that agree, rounded to two decimals.

The popcount form (count of 1 bits in the XOR) is what most languages expose for free, which is why this metric is the default bit-comparison tool in C, Java, and Python. When the two cleaned inputs have different lengths the calculator pads the shorter one with leading zeros so the alignment stays predictable; a hex 0F and a hex F therefore compare at length 2 with a leading zero on the shorter input.

Compare the binary messages 10101 and 01100

binary mode, message1 = 10101, message2 = 01100

Walk position by position. Position 1: 1 vs 0 (diff). Position 2: 0 vs 1 (diff). Position 3: 1 vs 1 (same). Position 4: 0 vs 0 (same). Position 5: 1 vs 0 (diff).

Distance: 3. Matching positions: 2. Compared length: 5. Similarity: 40.00%. Difference positions: 1, 2, 5.

Three of the five bits flipped, so the corruption is large enough that the original message is hard to recognise from the received one.

Compare the decimal messages 12271995 and 02071895

decimal mode, message1 = 12271995, message2 = 02071895

Position 1: 1 vs 0 (diff). Position 2: 2 vs 2 (same). Position 3: 2 vs 0 (diff). Position 4: 7 vs 7 (same). Position 5: 1 vs 1 (same). Position 6: 9 vs 8 (diff). Position 7: 9 vs 9 (same). Position 8: 5 vs 5 (same).

Distance: 3. Matching positions: 5. Compared length: 8. Similarity: 62.50%. Difference positions: 1, 3, 6.

Three of the eight digits changed, and the percentage makes the residual similarity readable without re-counting by hand.

According to Omni Calculator, the Hamming distance between 10101 and 01100 is 3 because the two 5-bit messages differ at positions 1, 2, and 5, and the decimal example 12271995 versus 02071895 also gives a distance of 3

According to Wikipedia, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different, and the canonical example karolin versus kathrin gives a Hamming distance of 3

The popcount shortcut (count of 1 bits in the XOR) is exactly the operation the Bitwise Calculator exposes for binary inputs, so the same AND, OR, XOR, and NOT truth table applies when you want to verify the per-bit comparison by hand.

Key Concepts Behind the Hamming Distance Calculator

Four small ideas explain every result, from a single bit to the longest allowed input.

Equal-length requirement

Both strings must have the same length after cleaning. If the inputs disagree, the calculator pads the shorter one with leading zeros so the position-by-position comparison still works.

Position-by-position count

Walk the two strings in lockstep and increment the counter at every position where the two characters disagree. For binary inputs that count equals the population count of the XOR.

Metric axioms

The Hamming distance satisfies d(x,x)=0, positivity, symmetry, and the triangle inequality. Symmetry is the property that lets the result survive input order swapping.

Minimum distance of a code

For an error-correcting code the minimum distance is the smallest Hamming distance between any two distinct codewords. A code with minimum distance 3 corrects any single-bit error.

Symmetry is the property that makes the result of this calculator interchangeable with the same calculation run on the inputs reversed. The popcount shortcut works because every position where the two bits agree contributes 0 to the XOR, and every position where they disagree contributes 1.

When the comparison length has to be cross-checked in another base, the Binary Converter translates the same 0 and 1 string into decimal, hex, and octal so the alignment rule and the padding behaviour can be inspected without retyping the input.

How to Use the Hamming Distance Calculator

Five steps move you from a pair of strings to a Hamming distance and a similarity percentage.

  1. 1 Pick the numeral system: Start on binary for the most common case. Switch to decimal, hex, or text when the inputs are digits or fixed-width words.
  2. 2 Paste message 1: Paste the first string. Spaces and a leading 0b (binary) or 0x (hex) marker are stripped automatically.
  3. 3 Paste message 2: Paste the second string. Use the same length as message 1 for the cleanest result.
  4. 4 Read the distance, matching count, and similarity: The result panel returns the distance, the matching-position count, the compared length, and the similarity percentage rounded to two decimals.
  5. 5 Inspect the difference positions list: The Difference Positions row lists every 1-based index where the two padded strings disagree.

You are auditing two checksum tokens: ABCDEF01 and ABCDEF02. Switch to hex, paste both into message 1 and message 2, and the panel returns distance 1, matching 7, length 8, similarity 87.50%, and difference positions: 8. The single flipped hex digit is the only change, so the audit trail is clean.

The single-bit parity check that flags an odd number of bit flips is what the Even Parity Calculator runs for any binary string, and that flag is the simplest sanity check before moving on to the per-position distance that this calculator returns.

Benefits of Using the Hamming Distance Calculator

Five practical benefits make the same metric useful for code review, audit trails, and pedagogy.

  • Four numeral systems in one tool: Binary, decimal, hex, and text modes share the same input field and the same distance algorithm.
  • Automatic leading-zero padding: When the cleaned inputs have different lengths the calculator pads the shorter one so the comparison stays predictable.
  • Visible difference positions list: The result panel returns a comma-separated 1-based list of every differing index.
  • Similarity percentage alongside the raw count: The panel returns the integer distance and the matching-percentage rounded to two decimals.
  • Direct link to the Hamming code family: The same metric surfaces the minimum-distance property the Hamming(7,4) code relies on.

The calculator doubles as a quick sanity check for any textbook example that lists the expected distance next to a code size, because the same algorithm scales from a 5-bit binary string to a 64-bit hex token. When the result is 0 the calculator confirms an exact match; when the result equals the length, every position differs and the strings are complements.

When the two inputs are actually text characters rather than raw bit patterns, the ASCII Converter turns the same string into the matching printable character or 7-bit code so the per-character difference can be inspected in plain English.

Factors That Affect Hamming Distance Calculator Results

Three factors decide which inputs the calculator accepts and how the result is computed.

Numeral system

Sets the allowed alphabet. Binary accepts 0 and 1 only; decimal accepts 0 to 9; hex accepts 0 to 9 plus A to F (case-insensitive); text accepts printable ASCII. An invalid character surfaces an error that names the allowed alphabet.

Input length after cleaning

After stripping spaces and the 0b / 0x marker, the comparison length is the longer cleaned length. The shorter cleaned input is padded with leading zeros so the alignment is well-defined.

Order of the two inputs

The metric is symmetric, so swapping message 1 and message 2 leaves the distance and similarity unchanged. The difference-positions list references the original input order.

  • The Hamming distance requires equal-length inputs. The calculator pads with leading zeros, but if you actually need the edit distance (insertions and deletions) you should switch to the Levenshtein distance instead.
  • The Hamming distance treats every position as independent, so it cannot tell you that 'Hamming' and 'humming' differ by a single phonetic shift. It only counts positions where the two strings disagree, character by character.

The leading-zero padding is the natural choice for binary and decimal, and the convention also applies to hex tokens, so the same alignment rule covers all four numeral systems. When the difference-positions list is the only thing you care about, focus on the raw count and the index list.

According to Wikipedia, Richard Wesley Hamming introduced the Hamming distance and the Hamming code framework in 1950 while at Bell Labs, with the metric later carrying his name

When the distance has to be recomputed against a transformed copy of one of the inputs, the Binary Operations Calculator adds, subtracts, multiplies, or divides the recovered binary number in the same register width so the new alignment can be re-checked against the original.

hamming distance calculator interface showing the numeral system selector, two equal-length message fields, and the resulting distance, matching positions, similarity, and difference-position list.
hamming distance calculator interface showing the numeral system selector, two equal-length message fields, and the resulting distance, matching positions, similarity, and difference-position list.

Frequently Asked Questions

Q: What is the Hamming distance between 10101 and 01100?

A: The Hamming distance is 3. Comparing 10101 with 01100 position by position, the bits differ at position 1, position 2, and position 5, so the count of differing positions is 3.

Q: How do you calculate the Hamming distance between two strings?

A: Make sure the two strings have the same length, then walk them in lockstep and increment a counter at every position where the two characters disagree. The final counter value is the Hamming distance, and the same number is the population count of the bitwise XOR of the two strings when the alphabet is binary.

Q: Is the Hamming distance a metric?

A: Yes. The Hamming distance satisfies the four metric axioms: d(x,x)=0, d(x,y)>0 when x!=y, d(x,y)=d(y,x), and the triangle inequality d(x,z) <= d(x,y) + d(y,z). That is why it is the standard yardstick for measuring distance between equal-length strings.

Q: What is the Hamming distance between two identical strings?

A: The Hamming distance is 0 when the two strings are identical, the matching-position count equals the string length, the similarity is 100.00%, and the difference-positions list is empty. That is the cleanest possible result.

Q: How does the Hamming distance relate to error-correcting codes?

A: The minimum distance of a code is the smallest Hamming distance between any two distinct codewords. A code with minimum distance 3 corrects any single-bit error, and the same property is what makes the Hamming(7,4) code a textbook error-correcting code.

Q: Can the Hamming distance be negative?

A: No. The Hamming distance is a count of differing positions, so it is always a non-negative integer between 0 and the length of the strings. Negative values would only be possible for a signed metric, and the Hamming distance is unsigned by construction.