Hamming is the metric for binary quantized vectors (1 bit per dim). Implemented as popcount(XOR(a, b)) using the hardware POPCNT instruction, which makes it 10-50x faster than float distance.
Imagine your vectors as bracelets. If your vectors are full of decimal numbers, you compare them with multiplications and additions. But if you squash each number down to just a single bit (positive or negative), the vectors are now bracelets of beads where each bead is either on or off. Comparing two bracelets becomes 'how many beads differ?', which is just XOR-ing the bracelets and counting the set bits. CPUs have a special instruction called POPCNT that counts set bits in a single cycle, so this whole comparison is almost free, way faster than the floating-point math you'd otherwise do.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Hamming distance is the oldest metric in computer science (Richard Hamming, 1950) and yet has become newly relevant in vector databases because of the binary-quantization renaissance of 2024-2026. The interview signal is whether the candidate connects three things: the metric definition, the data representation (binary vectors), and the implementation detail (POPCNT hardware instruction) that makes it dramatically faster than float distance.
Why Hamming pairs with binary data
Hamming distance is defined for equal-length bitstrings: dist(a, b) is the count of positions where the bits differ. It is a metric in the mathematical sense (non-negative, symmetric, satisfies triangle inequality), so it is a valid distance function for nearest-neighbor search.
It becomes the natural choice in a vector database when the vectors themselves are bitstrings, which happens via binary quantization (BQ). BQ takes a float32 embedding and stores only the sign bit of each dimension after mean-centering. A 1024-dim float vector becomes a 1024-bit packed bitstring (128 bytes), a 32x storage compression.
Once the vectors are binary, you can't compute cosine distance on them (cosine assumes continuous-valued dimensions). Hamming is the canonical replacement: count the bits that differ. The semantic interpretation is 'how many dimensions disagree on sign', which preserves the signal from float distance if the embedding model has been trained for it.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
| Property | Hamming on BQ | Cosine on float32 | Cosine on int8 SQ |
|---|---|---|---|
| Bytes/vec (1024-dim) | 128 | 4096 | 1024 |
| Distance kernel | popcount(XOR) | Float MUL-ADD chain | int8 SIMD dot product |
| CPU cycles per pair | ~30-100 | ~250-500 | ~80-200 |
| Recall impact (binary friendly embeddings) | 1-3 pts loss | Baseline | <1 pt loss |
| Recall impact (general embeddings) | 10-20 pts loss | Baseline | <1 pt loss |
| When to use | Binary-aware models + speed/cost binding | Default | Cheap cost reduction |
Real products, models, and research that use this idea.
- Qdrant exposes binary quantization with Hamming as a first class collection setting, with two stage retrieval as the recommended pattern.
- Weaviate supports binary quantization on HNSW indexes for cost sensitive deployments.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does AVX-512's VPOPCNTDQ matter more for billion-scale binary search than for typical applications?
It processes 512 bits per cycle, so a 1024-bit Hamming distance reduces to 2 SIMD ops instead of 16 scalar ones. At billion-vector first stage scan, this 8x kernel-level speedup shows up as a 4-6x end to end QPS improvement.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating Hamming as a general purpose vector metric. Hamming only makes sense on binary or binary quantized data; on float vectors it loses to cosine or L2 by a huge margin in semantic accuracy.
60 second bullets to scan on the way to the call.
Hamming is defined as popcount(XOR(a, b)) on bitstrings
Pairs with binary-quantized embeddings (1 bit per dim, 32x storage reduction)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.