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.
Detailed answer & concept explanation~4 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: define Hamming + connect to binary quantization + name POPCNT as the speed mechanism + cite vendor support (Qdrant, Weaviate, Milvus) + mention two stage retrieval pattern.
| 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.
- Milvus ships BIN_FLAT and BIN_IVF_FLAT indexes for explicitly binary data with Hamming, Jaccard, and Tanimoto metrics.
- Cohere Embed v3 binary mode produces sign stable embeddings that retain recall under BQ; used by RAG products like Notion and Coda.
- Image-deduplication systems (TinEye, Google Images backend hints) use pHash + Hamming for visual similarity at petabyte scale.
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?
QHow do Cohere binary embeddings differ from running BQ on a generic float embedder?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.