Hamming Distance Calculator

Count positions where two equal-length strings or bit patterns differ. This educational calculator shows the formula, result, and step-by-step interpretation.

Computer ScienceCoding TheoryDiscrete Math

Calculator

What this calculator teaches

Hamming distance is used in coding theory, error correction, clustering, and string comparison.

Use the result as a learning aid. For classwork, still show the formula and intermediate reasoning so the final answer is not just a black-box number.

Hamming Distance Measures Position-by-Position Mismatch

Hamming distance is the number of positions at which two equal-length sequences differ. It does not measure how far symbols are numerically or alphabetically apart; each position contributes either 0 for a match or 1 for a mismatch. For binary strings, the distance is also the number of 1-bits in the XOR of the two bit patterns.

This simple metric is fundamental in coding theory. If valid codewords are separated by minimum Hamming distance dmin, a code can detect up to dmin−1 symbol errors in a received word. It can correct up to floor((dmin−1)/2) errors under nearest-codeword decoding because correction requires the error pattern to remain closer to the intended codeword than to any other.

CaseRuleInterpretation
General stringsd(a,b)=Σ[aᵢ≠bᵢ]Count mismatched positions
Binary stringsd = popcount(a XOR b)Count differing bits
Identical stringsd=0No positional mismatches

Equal length is part of the standard definition. If insertion and deletion operations are allowed, edit distance or Levenshtein distance is usually the more appropriate metric.

Hamming distance is a true metric on equal-length strings: it is never negative, equals zero only for identical sequences, is symmetric, and satisfies the triangle inequality. These properties make it useful for nearest-neighbor decoding and for comparing fixed-width hashes, bit vectors, and categorical codes.

In coding theory, minimum code distance controls protection capability. If dmin=3, a code can detect up to 2 errors and correct 1 error. If dmin=5, it can detect up to 4 and correct up to 2. This does not mean any arbitrary set of strings is an error-correcting code; the separation is a designed property of the valid codewords.

Formula & Symbols

ConceptFormula or rule
Hamming distanced(x,y) = number of positions where xᵢ ≠ yᵢ

Worked example

Example: 101101 and 100111 differ at two positions, so the Hamming distance is 2.
Example 2: 1011101 and 1001001 differ at positions 3 and 5, so their Hamming distance is 2.
Example 3: “karolin” and “kathrin” differ at three positions, giving distance 3.
Example 4: 1010 and 0101 differ in every position, so the distance is 4.
Example 5: ABCDEF compared with ABCDEF has Hamming distance 0.

Common mistakes

⚠️
Using the wrong input format

Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.

⚠️
Ignoring assumptions

Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.

⚠️
Comparing unequal-length sequences

Standard Hamming distance assumes one-to-one aligned positions. Use an edit-distance method when insertions or deletions are part of the problem.

⚠️
Counting magnitude instead of mismatches

A change from 0 to 9 still contributes one mismatch, just like a change from 0 to 1. Hamming distance is categorical by position.

FAQ

Can I use decimals?
Most numerical calculators allow decimals, but modular arithmetic and coding-theory tools usually require integers or binary strings.
Is this for homework checking?
Yes. The page is designed to show both the answer and the reasoning pattern.
Why does the result sometimes say approximate?
Some probability, floating-point, and numerical methods naturally produce approximations.
What is the maximum Hamming distance?
For two sequences of common length n, the maximum distance is n, reached when every aligned position differs.
How is Hamming distance related to XOR?
For binary strings, XOR produces 1 exactly where the bits differ. Counting those 1s therefore gives the Hamming distance.
Can Hamming distance correct errors?
The distance itself is a metric. Error-correcting codes use sufficiently separated codewords so that a received word can be mapped back to the nearest valid codeword.
Is Hamming distance the same as edit distance?
No. Hamming distance counts substitutions at fixed aligned positions and requires equal length; edit distance can also account for insertions and deletions.

Related calculators

These links will work after the calculators are registered in the final Math layout update.

Formula Explorer connections

Interpretation: This formula uses integer divisibility, modular arithmetic, finite fields or coding relationships. Assumption: Inputs are usually integers with specific modulus or coprimality requirements. Cryptographic examples are educational and not a substitute for vetted security libraries.

Luhn Algorithm Calculator →Modular Arithmetic Calculator →Modular Inverse and Extended Euclidean Calculator →Math Formula Explorer →