Skip to content
uencoder

Hamming Distance

The Hamming distance is the number of positions at which corresponding characters differ between two equal-length strings; it only applies to equal-length comparisons.

Purpose

Measures the number of differing positions between equal-length strings, used in error detection and coding theory.

Mechanism

For equal-length strings s and t, d = Σ(s[i] ≠ t[i]). Defined only when lengths are equal. For binary strings the Hamming distance can be computed efficiently with bitwise operations (XOR + popcount).

Use cases

  • Error detection and error-correcting codes
  • Binary string similarity
  • Perceptual difference of image hashes
  • Information theory education

Examples

  • Input
    karolin → kathrin
    Output
    3

    3 positions differ

Common errors

Applying the Hamming distance to strings of unequal length
Use the Levenshtein distance for variable lengths

Related terms

Related tools

References