Hamming Distance Calculator
How different are two strings of the same length, position by position, symbol by symbol? Hamming distance gives the answer as a single count: the number of positions where they disagree. It is the founding metric of error-correcting codes and coding theory itself, a workhorse of genetics and information theory, and a genuinely useful way to compare sequences, words and bit patterns. Enter two strings below — binary, text, digits, anything — and the calculator reports the distance, the match percentage, and the differing positions themselves. It is the fastest useful question in all of coding theory, asked and answered in one line of arithmetic.
A Metric With a Birthday and a Purpose
Richard Hamming defined the distance in 1950, at Bell Labs, out of frustration: the weekend computer runs kept failing on single flipped bits, and he wanted a mathematics of how far apart valid messages must sit so that damage is recognisable — and repairable. The definition could hardly be simpler: align two equal-length strings and count the positions that differ. But the simplicity conceals a genuine metric in the mathematical sense — non-negative, symmetric, zero only for identical strings, and obeying the triangle inequality — which licenses geometric intuition: codewords are points, distance is separation, an error is a step toward a neighbouring point, and correction is the decision of which point you most plausibly started from. Everything in coding theory — the parity bit, the Hamming code built beside this page, the Reed–Solomon codes in every QR scan — is this counting idea, industrialised.
Worked Examples: Names, Bits and DNA
The defaults are Hamming's own textbook pair: karolin against kathrin — positions 3, 4 and 5 differ (r/t, o/h, l/r), distance 3, similarity 4/7 ≈ 57.1%. In binary dress: 1011101 against 1001001 disagree at positions 3 and 5 only — distance 2 — the arithmetic a modem's error checker performs mentally at line speed. And in biology's alphabet: two DNA fragments ACGTACGT and ACGTTCGA differ at positions 5 and 8 (distance 2), the primitive under every sequence-alignment statistic — mutation counting before the fancier edit distances take over. Three alphabets, one act of counting, because the metric cares about positions, not symbols: it is the purest possible notion of "how much editing separates these", restricted to the substitution-only world where lengths agree.
What the Distance Buys: Detection and Correction Budgets
The engineering content of a code's minimum distance d — the smallest Hamming distance between any two of its valid codewords — is a pair of budgets every communications student memorises: the code can detect up to d − 1 errors (no error pattern of that size can land on another valid word) and correct up to ⌊(d − 1)/2⌋ (the damaged word remains closer to its origin than to any rival). A single parity bit buys d = 2: detection of one error, correction of none. The (7,4) Hamming code buys d = 3: correct one, detect two. Modern codes buy d in the dozens and spend it on scratched discs and noisy deep-space links. Whenever a system quotes its error performance, this page's count is the currency — and the calculator's per-position report shows exactly which positions an error pattern would have to flip to spend it.
Hamming Versus Its Relatives
Three neighbouring distances cause most of the confusion, and the distinctions are worth a paragraph. Levenshtein (edit) distance allows insertions and deletions as well as substitutions, so it handles unequal lengths — spell-checkers live there; Hamming is its equal-length specialisation. Jaccard distance compares sets (overlap of membership), ignoring positions entirely — the same symbols in scrambled order give Jaccard zero and Hamming large, and the two answer different questions ("same ingredients?" versus "same arrangement?"). Euclidean and Manhattan distances operate on numbers, weighting large deviations; Hamming counts a position as wholly right or wholly wrong, the correct temperament for symbols, where a digit off by one is not "closer" than a digit off by nine in any sense a decoder can use. Choosing among them is choosing what "different" means for your data — and for fixed-length codes, sequences and strings, the count on this page remains the founding answer.
Hamming Weight, and One More Worked Count
A useful relative comes free with the definition: the Hamming weight of a string is its distance from the all-zeros string of the same length — simply its count of non-zero symbols. The weight of the bit string 10110011 is 5 (ones at positions 1, 3, 4, 7, 8), and weights run error detection's back office: constant-weight codes (every codeword the same weight) expose any error that changes the count, which is one reason barcodes and railway signalling have used them. The worked count, to close the section: two eight-bit sensor readings, 10110011 against 10011011, disagree at positions 3 and 5 only — distance 2, weight 5 against weight 5 — a pair that a single parity bit would flag as damaged but could not arbitrate, which is precisely the capability gap the Hamming code next door was invented to close. Keep the pattern in view: distance counts the damage; the whole art of coding is spending redundancy so that this count, computed at the receiver, identifies the wound rather than merely announcing one.
Distance at Machine Speed: XOR and Popcount
For binary strings the distance has a hardware-fast identity worth knowing: distance(a, b) = popcount(a XOR b) — XOR marks exactly the differing positions with 1s, and popcount (population count, a single instruction on modern processors) tallies them. The worked pair from the previous section demonstrates it: 10110011 XOR 10011011 = 00101000, whose popcount is 2 — the distance, computed by two machine operations on words of any length. This identity is why Hamming distance, alone among string metrics, is effectively free at scale: comparing millions of hashes, fingerprints or genomic k-mers for near-duplicates is XOR-and-count, billions of times a second. The 1950 definition turned out to be the one the hardware wanted.
From Distance to Error Rates
One scaling step connects this page's count to the numbers network engineers quote: a channel's bit error rate (BER) is the long-run fraction of positions flipped in transit — an expected Hamming distance per bit. At a BER of 10⁻⁶ (one flipped bit per million, a respectable link), a line moving 10⁹ bits a day accumulates about 1,000 single-bit errors daily, each one a distance-1 step from the transmitted word. No code corrects its way out of remembering such a channel at all; the code's minimum distance merely decides whether those thousand daily steps are detected, repaired, or delivered as data. Hamming's count is the unit in which channel quality, code design and expected damage are all, ultimately, denominated — one count, three professions, no translation losses anywhere along the chain.
Related Tools
More from the Other Calculators hub: the Hamming Code Calculator — the code this metric made possible — and the live Parity Bit Calculator, the distance-2 special case in widget form.
Frequently Asked Questions
Why must the strings be the same length?
Because the definition is positional: position i of one string is compared with position i of the other, and an unmatched tail has no counterpart to disagree with. Real data of unequal length needs alignment decisions (which gaps to open where) that belong to edit distance and sequence alignment — richer tools with richer assumptions. Hamming's restriction is its honesty: where lengths match, no alignment judgement contaminates the count.
Is the comparison case-sensitive?
Yes, as it should be for code-like data: "A" and "a" are different symbols occupying a position, and they count. For word comparisons where case is noise, lower-case both strings before entering them — the calculator counts what it is given, and the one-line discipline of normalising case (and whitespace, which also counts as characters) belongs to the analyst preparing the strings.
What does distance mean for a code's quality?
Everything structural: a code's minimum pairwise distance sets its detection budget (d − 1 errors) and correction budget (⌊(d−1)/2⌋), as the section above details. When you evaluate a proposed encoding, the minimum Hamming distance between codewords — computable by pairwise application of this page's count — is the first number to demand, before efficiency, before elegance.
Can Hamming distance be used on numbers?
On their representations, yes and usefully: the bit strings of two integers (their XOR's population count is the Hamming distance) underlie everything from the parity of chessboards in puzzle theory to locality-sensitive hashing. On the numeric values themselves, no — 100 and 099 differ numerically by 1 but their strings differ in two positions, and which answer you want depends on whether the numbers are quantities or codes.
How is this used in genetics?
As the zeroth-order divergence measure between aligned sequences of equal length — the raw count of differing sites, ancestor of the p-distance and (corrected for multiple hits at the same site) of the evolutionary distances phylogenetics actually publishes. Its naivety is its role: it is the number every fancier model starts from and must beat.
What is the similarity percentage, exactly?
The complement of the distance as a share of length: (L − d)/L × 100. It is a reporting convenience, not a standard statistic — two strings of length 100 at distance 3 and two of length 7 at distance 3 share a distance and nothing else, which the similarity figure (97% versus 57%) states more honestly than the bare count. Quote both numbers; the pair is the description.