◄ WORLD V · SONNY 5DART 534 · a helldive at the table

THE MULTIPLICATIVE HASH multiply, keep the high bits

Knuth’s multiplicative hash: multiply the key by a big odd constant A, keep the low w bits, and read the high bits of those as the bucket. The high bits mix contributions from the whole key, so close keys scatter. With A near 2^32/φ it is Fibonacci hashing. One multiply, one shift, no division. Here 42 and 43 land in buckets 245 and 147 — adjacent keys, far apart buckets.

THE TECHNIQUE h = (k·A mod 2^32) >> (32−bits)

The demo multiplies by A = 2654435761, masks to 32 bits, and shifts to an 8-bit bucket: live demo


HISTORY & CREDIT Knuth · TAOCP vol 3

“You need a prime modulus to hash well.” — multiplicative hashing uses a power-of-two modulus (a mask) and puts the mixing in the multiply. cited

the multiply · k·A spreads the key across all bits.
the shift · the high bits of the low word are the most mixed — take those.
Knuth · Donald Knuth, The Art of Computer Programming vol. 3 (1973); A near 2^32/φ = Fibonacci hashing.

A multiply and a shift — the cheapest good hash. arithmetic

RECOMMEND FOR I-13 the two buckets, on the compiler

On i-13, 42 and 43 map to buckets 245 and 147 (32-bit width stays exact in f64: 43·A < 2^53):

$ i13 run hs_multiplicative.i13 RUN OK · 39 step(s) · peak stack 3 bucket42 = 245 bucket43 = 147 spread = 1 -- adjacent keys, different buckets
Recommend as a NULL — arithmetic (B40) + a pinned function (B39). The bucket is a deterministic function of the key; two correct multiplicative hashers with the same A return the same bucket. A better constant is a resource choice (fewer collisions), not a new invariant. NULL — one multiply, one shift.