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.
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.
| Case | Rule | Interpretation |
|---|---|---|
| General strings | d(a,b)=Σ[aᵢ≠bᵢ] | Count mismatched positions |
| Binary strings | d = popcount(a XOR b) | Count differing bits |
| Identical strings | d=0 | No 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
| Concept | Formula or rule |
|---|---|
| Hamming distance | d(x,y) = number of positions where xᵢ ≠ yᵢ |
Worked example
Common mistakes
Keep lists comma separated, matrices as rows separated by semicolons, and modular inputs as integers.
Some methods require positive probabilities, valid moduli, independent trials, or small educational input sizes.
Standard Hamming distance assumes one-to-one aligned positions. Use an edit-distance method when insertions or deletions are part of the problem.
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
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.