Skip to content
K Knidox Search…
Computer Science · Coding Theory

Hamming Distance Calculator

Count the positions where two equal-length strings differ — for binary codewords or plain text.

Binary, text, or any characters.
Must be the same length as A.
Try an example
Hamming distance
3equal length

Length 7; the characters differ at indices 2, 3, 4 (0-based).

Aligned comparison — highlighted columns differ

karolin
kathrin

3 columns differ.

The Hamming distance between two equal-length strings is the number of positions at which their characters differ. Compare them position by position and count the mismatches. For “karolin” and “kathrin”, the letters differ at indices 2, 3, and 4 (r/t, o/h, l/r), so the Hamming distance is 3.

What the Hamming distance measures

Named after Richard Hamming, the distance counts how many single-character edits — substitutions only — separate one string from another of the same length. Line the two strings up, walk through them one position at a time, and add one for every position where the characters disagree. It works on anything: binary codewords such as 1011101 and 1001001, DNA bases, or ordinary words. The one firm requirement is that both strings have the same length, because the metric is defined position-by-position and there is no meaningful comparison once the strings drift out of alignment.

d(A, B) = number of indices i where A[i] ≠ B[i]

A and B must have the same length; count every position whose characters differ.

Worked example: “karolin” vs “kathrin”

Both words have seven letters, so the comparison is well defined. Check each index in turn.

  1. 1
    Check the lengths match. “karolin” and “kathrin” both have 7 characters, so a Hamming distance exists.
  2. 2
    Align the strings and index from 0. k(0) a(1) r(2) o(3) l(4) i(5) n(6) against k a t h r i n.
  3. 3
    Compare each position. Indices 0, 1, 5, and 6 match; index 2 is r vs t, index 3 is o vs h, index 4 is l vs r.
  4. 4
    Count the mismatches. Three positions differ: indices 2, 3, and 4.
  5. 5
    Read off the distance. The Hamming distance is 3.

Example pairs and their Hamming distance

Each pair is the same length; the distance is the count of differing positions.

String AString BDiffering indicesDistance
karolinkathrin2, 3, 43
101110110010012, 42
10101010(none)0
tonedroses0, 2, 43

Why it matters in coding theory

The Hamming distance is the backbone of error-detecting and error-correcting codes. If a transmitted codeword picks up bit errors, the received word lands some Hamming distance away from the original. The minimum Hamming distance of a code — the smallest distance between any two valid codewords — sets its power: a code with minimum distance d can detect up to d − 1 errors and correct up to ⌊(d − 1) ÷ 2⌋ of them. That is why parity checks, Hamming codes, and modern schemes are all designed to spread their valid codewords as far apart as possible. Remember the metric only applies to equal-length strings; when insertions or deletions can shift characters, the Levenshtein (edit) distance is the right tool instead.

What is the Hamming distance used for?
It is the core measure in coding theory for error detection and correction. Comparing a received message against valid codewords by Hamming distance reveals how many bits were corrupted and, for the nearest codeword, how to correct them. It is also used in cryptography, information theory, and comparing DNA or fixed-length records.
Why must the two strings be the same length?
The Hamming distance is defined position by position: index 0 against index 0, index 1 against index 1, and so on. If the strings differ in length there is no aligned partner for the extra characters, so the count is undefined. For unequal-length strings, use the Levenshtein (edit) distance instead.
How is the “karolin” vs “kathrin” distance 3?
Both words have seven letters. Comparing each index, they match at 0, 1, 5, and 6 but differ at index 2 (r vs t), index 3 (o vs h), and index 4 (l vs r). Three positions differ, so the Hamming distance is 3.
Does it count how the characters differ, or just that they differ?
Only that they differ. Each mismatched position adds exactly 1 regardless of which characters are involved, so a vs b and a vs z both count as a single difference. The distance is purely the number of disagreeing positions.
What does the minimum Hamming distance of a code tell you?
It is the smallest distance between any two valid codewords and sets the code’s strength. A code with minimum distance d can detect up to d − 1 errors and correct up to ⌊(d − 1) ÷ 2⌋ errors, which is why good codes keep their codewords far apart.
How is the Hamming distance different from the edit distance?
Hamming only allows substitutions and requires equal-length strings, counting mismatched positions. The Levenshtein edit distance also allows insertions and deletions, so it can compare strings of different lengths. For same-length inputs the Hamming distance is always greater than or equal to the edit distance.