What Is Hamming Distance?
The Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols differ. Named after Richard Hamming, it is a fundamental concept in information theory, coding theory, and computer science. It measures the minimum number of substitutions required to change one string into the other, and is widely used in error-detecting and error-correcting codes.
How to Use This Calculator
Enter your two strings — they can be binary sequences (like 1011101), DNA bases, or any text. The calculator compares them character by character and counts how many positions differ. If the strings are different lengths, every extra character in the longer string counts as a difference.
The Formula Explained
For two strings a and b, the Hamming distance is the sum over all positions i of an indicator that equals 1 when ai ≠ bi and 0 otherwise:
$$d(a, b) = \sum_{i=1}^{\min(|a|,|b|)} \left[\, \text{A}_i \neq \text{B}_i \,\right] + \Big|\; |\text{A}| - |\text{B}| \;\Big|$$
The result is always a non-negative integer between 0 (identical) and the string length (every position differs).
Worked Example
Compare 1011101 and 1001001. Lining them up:
10111011001001
Positions differ at index 3 (1 vs 0) and index 5 (1 vs 0). That gives a Hamming distance of 2.
FAQ
Must the strings be the same length? Classic Hamming distance requires equal length. This tool still gives a useful answer for unequal strings by counting each surplus character as a mismatch.
Does it work for non-binary text? Yes. Any characters can be compared — letters, digits, or symbols.
What is the difference from Levenshtein distance? Hamming only counts substitutions at fixed positions, while Levenshtein also allows insertions and deletions.