Hamming Distance

6 min read#information

The number of positions in which two equal-length strings differ — the metric behind error correction.

Contents

Hamming Distance

The Hamming distance between two strings of equal length is simply the number of positions where they disagree. For two binary words it is the count of bits you would have to flip to turn one into the other:

d_H(x, y) = \#\{\, i : x_i \neq y_i \,\} = \sum_i (x_i \oplus y_i).

For example, 1011101 and 1001001 differ in positions 3, 5, and 6, so their Hamming distance is 3. It is a genuine metric — non-negative, symmetric, zero only for identical strings, and obeying the triangle inequality — which is what lets us reason about codes geometrically, as points scattered in a space of strings.

Why it sets correcting power

A code is just a chosen set of valid codewords. Its minimum distance d_{\min} — the smallest Hamming distance between any two codewords — determines everything about its robustness. Picture each codeword sitting at the center of a ball of nearby strings; if the balls don't overlap, a received word that falls inside one gets decoded to its center.

Distance as a cube

For 3-bit strings, every word is a corner of a cube and Hamming distance is the number of edges of the shortest path between corners. Hover a corner to highlight its neighbors at each distance — adjacent corners differ by 1 bit, face-diagonals by 2, the body-diagonal by 3.

The 3-bit cube. Each corner is a binary word; Hamming distance is the edge-count between corners. Move the mouse near a corner to color all others by their distance from it (green = far, blue = near).

Beyond bits

Hamming distance is not only for codes. It measures how many single-character edits separate two DNA strands, quantifies how different two hash fingerprints are, and serves as a similarity metric in machine learning whenever data is categorical. Anywhere you compare strings position by position, it is the natural ruler — and it is the foundation on which every Error-Correcting Code is designed.

Two codewords have Hamming distance 5. How many bit errors can a code with this as its minimum distance correct?

See also