Hamming Distance Calculator
Find the Hamming distance between two equal-length inputs — binary codewords, text strings, or numeric vectors — as the count of positions that differ. See the normalised Hamming loss, the similarity, and exactly which positions mismatch. Runs entirely in your browser, no signup.
How it works
The Hamming distance between two sequences A and B of equal length n is the number of positions at which their symbols differ. It was introduced by Richard Hamming in his 1950 paper on error-detecting and error-correcting codes, and it is one of the building blocks of coding theory: a block code whose codewords are all at least a Hamming distance d apart can detect up to d − 1 errors and correct up to ⌊(d − 1) / 2⌋.
- Parse each input into a list of symbols. Binary mode keeps each 0/1 character (and rejects anything else); text mode keeps each Unicode character, optionally lower-cased; vector mode splits on a comma or space and parses each element as a number, so 2 and 2.0 compare equal.
- Length guard. If the two inputs parse to different lengths, the distance is undefined, so the tool stops and shows an error rather than padding the shorter one.
- Count the mismatches with the core formula, where [A_i ≠ B_i] is 1 when the symbols at position i differ and 0 otherwise:
d_H(A, B) = Σ [ A_i ≠ B_i ] for i = 0 … n−1
- Normalise. The normalised Hamming distance, or Hamming loss, is d_H / n— the fraction of positions that differ, between 0 and 1. This matches scikit-learn's hamming_loss and SciPy's distance.hamming.
- Similarity is 1 − d_H / n, shown as a percentage — the share of positions that agree. When both inputs are empty the loss is defined as 0 and similarity as 100%, avoiding a division by zero.
The integer distance is exact with no rounding; only the normalised value and similarity are shown to a fixed number of decimals for display. Every result is independently cross-checked against a second, reduce-based implementation of the same count — if the two ever disagreed, the badge in the tool would flag it.
Worked examples
Frequently asked questions
Sources & references
- Hamming, R. W. (1950) — Error detecting and error correcting codes (Bell System Technical Journal 29:147–160)
- scikit-learn — metrics.hamming_loss (normalised Hamming distance / Hamming loss)
- SciPy — scipy.spatial.distance.hamming reference
The definition, formula, and worked examples on this page were last cross-checked against these sources on 2026-06-10. Every distance is deterministic and verified against an independent second implementation on each calculation.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.