What is Hamming distance?

Hamming distance counts the number of positions where two equal-length vectors differ, making it the standard way to compare binary or quantized vector representations.
Created: Updated: 3 min read

Hamming distance measures how many positions differ between two vectors of equal length, rather than measuring a continuous geometric distance the way Euclidean distance or cosine similarity do, which makes it the natural fit for binary data where each position simply holds one of two possible values.

How is Hamming distance actually calculated?

Compare the two vectors position by position, count how many positions hold different values, and that count is the Hamming distance. Two identical vectors have a Hamming distance of 0, and two vectors that differ in every single position have a Hamming distance equal to their full length. Unlike the continuous distance functions covered elsewhere in this glossary, there’s no notion of “how much” two positions differ — each position either matches or it doesn’t, and every mismatch counts equally regardless of what the actual differing values happen to be.

Why is Hamming distance the natural fit for binary vectors specifically?

Continuous distance functions like Euclidean distance and cosine similarity assume values can vary smoothly across a meaningful numeric range, an assumption that simply doesn’t apply to binary data where every value is either a 0 or a 1 with nothing meaningful in between. Applying a continuous distance function to binary data can still be computed, but the result doesn’t carry the same geometric meaning it would for continuous values — Hamming distance instead measures binary data in the way that actually matches how it’s structured, counting simple agreement or disagreement at each position rather than measuring a continuous gap that doesn’t really exist between two binary values.

How does Hamming distance connect to compression techniques covered elsewhere on this site?

Some vector compression schemes, covered in this site’s compression-and-quantization section, deliberately convert continuous, full-precision vectors into compact binary representations specifically to save memory and speed up comparisons. Once vectors have been compressed this way, comparing them naturally shifts from whatever continuous distance function was used on the original vectors to Hamming distance instead, since the compressed representation is now binary. This trade-off sacrifices some precision in exchange for a much smaller memory footprint and a comparison that’s often faster to compute than continuous distance functions, since comparing binary values position by position is a simpler operation than the multiplication and accumulation continuous distance functions require.

Hamming distance is the natural counterpart to the continuous distance functions covered elsewhere in this glossary, specifically suited to binary data. From here, this site’s compression-and-quantization glossary section covers the specific techniques that produce binary vectors in the first place, and the page on Jaccard similarity covers a related measure built for set-based rather than fixed-length binary data.