Cryptography & Security
Basic

Hamming Distance

Number of positions at which two strings differ.

Formula

d(x,y)=∑i[xi≠yi]d(x,y) = \sum_i [x_i \ne y_i]

Variables

dHamming distance
x_ii-th symbol of x
y_ii-th symbol of y

Example

1011 vs 1001: differ in one bit → d = 1

How it works

Hamming distance counts how many symbol positions are different when two strings have the same length. The sum uses the indicator [x_i \ne y_i], which contributes 1 whenever the symbols at position i do not match and 0 when they do, so adding over all positions gives the total number of mismatches. This is useful because it turns a comparison problem into a simple count.

When to use it

Use Hamming distance whenever you need to measure how similar two equal-length strings, bit patterns, or codewords are. It is common in error detection/correction, digital communications, genetics, and comparing encoded data.

Worked example, step by step

Compare x = 110010 and y = 100110 Step 1: Align the symbols by position: 1 vs 1 → same, contributes 0 1 vs 0 → different, contributes 1 0 vs 0 → same, contributes 0 0 vs 1 → different, contributes 1 1 vs 1 → same, contributes 0 0 vs 0 → same, contributes 0 Step 2: Add the contributions: d(x,y) = 0 + 1 + 0 + 1 + 0 + 0 Step 3: Final result: d(x,y) = 2

Common mistakes to avoid

  • Using strings of different lengths, which is not valid for standard Hamming distance
  • Counting how many characters are present in one string but not the other instead of comparing positions
  • Forgetting that each differing position counts as exactly 1, no matter which symbols appear there

Did You Know?

Richard Hamming devised his distance in 1950 after weekend computer crashes kept wrecking his jobs.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026