The Hamming distance between two strings of the same length is the number of places where they differ. Between 1011101 and 1001001 it is 2; between “karolin” and “kathrin” it is 3. Richard Hamming introduced it in 1950 to measure how many bit errors separate one codeword from another, and it remains the basic yardstick of coding theory: a code whose codewords are at least d apart can detect d − 1 errors and correct ⌊(d − 1)/2⌋.
The idea is useful far beyond telecommunications. Bioinformatics compares DNA sequences of equal length by counting substitutions, image-hashing and near-duplicate detection compare fingerprints by the number of differing bits, and machine learning uses it as a distance between binary feature vectors. Typing errors, wrong digits in ID numbers and corrupted transmissions are all measured the same way.
Hamming distance only counts substitutions, so both strings must be the same length – an insertion or deletion would shift every later character. For strings of different lengths use the Levenshtein (edit) distance instead. In hexadecimal mode the calculator gives both the number of differing hex digits and the number of differing bits, since one wrong hex digit can hide up to four flipped bits.
How to use the Hamming Distance Calculator
- Compare as
- First string
- Second string
Everything is calculated in your browser, so results appear instantly and nothing you type is sent to a server.
How it works
The strings are compared character by character and every mismatch is counted and marked with ^. In integer mode both numbers are converted to binary of equal width and XOR-ed; the distance is the number of 1s in the result.
Worked example
Compare 1011101 and 1001001. Position by position: 1/1, 0/0, 1/0 ✗, 1/1, 1/0 ✗, 0/0, 1/1. Two positions differ, so d = 2 and 5 of 7 positions (71.4%) match. As decimal digit strings, 12271995 and 02071895 differ in positions 1, 3 and 6, so d = 3.
Minimum distance and error control
| Minimum distance d | Detects up to | Corrects up to | Example |
|---|---|---|---|
| 2 | 1 error | 0 | single parity bit |
| 3 | 2 errors | 1 error | Hamming(7,4) |
| 4 | 3 errors | 1 error (+ detects 2) | extended Hamming (SECDED) |
| 5 | 4 errors | 2 errors | BCH codes |
Formula
d(x, y) = number of positions i where xᵢ ≠ yᵢ
For bit strings: d = popcount(x XOR y) · similarity = (n − d) ÷ n
Worked example
Inputs — Compare as: Binary strings; First string: 1011101; Second string: 1001001.
Results — Hamming distance = 2; Similarity (matching positions) = 71.4 %; Positions that differ = 3, 5; Distance in bits = 2; Comparison = 1011101
1001001
^ ^ ; Length compared = 7.
Key terms
- Hamming distance
- Number of positions at which two equal-length strings differ.
- XOR
- Exclusive or: 1 where two bits differ, 0 where they match.
- Popcount
- The number of 1 bits in a binary number (Hamming weight).
- Levenshtein distance
- Edit distance that also allows insertions and deletions.
Frequently asked questions
What is the Hamming distance between 1011101 and 1001001?
2 – they differ in positions 3 and 5.
Can strings of different lengths be compared?
No. Hamming distance needs equal lengths; use the Levenshtein distance for insertions and deletions.
What is the Hamming distance between “karolin” and “kathrin”?
3 (r/t, o/h, l/r).
How is it related to error correction?
A code with minimum distance d detects up to d − 1 errors and corrects up to ⌊(d − 1)/2⌋. Hamming(7,4) has d = 3.
How do I compute it for numbers?
XOR the binary forms and count the 1s: 93 XOR 73 = 0010100, which has 2 ones.
What is the maximum possible distance?
The length n, when every position differs, e.g. 010101 vs 101010 has distance 6.
Is Hamming distance a metric?
Yes – it is non-negative, symmetric, zero only for identical strings and obeys the triangle inequality.