@TrisH0x2A: a hash map in 25 lines of C FNV-1a handles the hashing, start with a fixed value and XOR each byte multiply by a prime …

X AI KOLs Timeline Tools

Summary

A concise implementation of a hash map in 25 lines of C using FNV-1a hashing and bitwise AND for efficient modulo operation, with void pointers for type-generic storage.

a hash map in 25 lines of C FNV-1a handles the hashing, start with a fixed value and XOR each byte multiply by a prime and repeat the interesting part is the last line instead of using hash % cap it uses hash & (cap - 1) same result when the capacity is a power of two and a bitwise AND is generally cheaper than a modulo operation another neat detail is the use of void pointers for values the hash map does not care what type you store integers strings structs anything works the caller handles the casting when storing and retrieving data
Original Article
View Cached Full Text

Cached at: 06/25/26, 05:19 AM

a hash map in 25 lines of C

FNV-1a handles the hashing, start with a fixed value and XOR each byte multiply by a prime and repeat

the interesting part is the last line

instead of using hash % cap it uses hash & (cap - 1)

same result when the capacity is a power of two

and a bitwise AND is generally cheaper than a modulo operation

another neat detail is the use of void pointers for values

the hash map does not care what type you store

integers strings structs anything works

the caller handles the casting when storing and retrieving data

Similar Articles

SequenceHash: multihashing for the rest of us

Lobsters Hottest

Trail of Bits introduces SequenceHash and SequenceMAC, open-source multihashing constructions that work with any standard cryptographic hash function (SHA2, BLAKE, RIPEMD) rather than being tied to Keccak like NIST's TupleHash. The C2SP-approved spec ships with Rust, Go, and Python implementations plus test vectors, aimed at preventing ambiguous-input attacks in protocols like the Fiat-Shamir transform.

Heavenly delights of hashing numbers under EQUALP

Lobsters Hottest

A technical deep-dive into optimizing hashing of numbers under the EQUALP predicate in ECL (Embedded Common Lisp), covering new hash table and hash function implementations that significantly speed up hash tables and generic function dispatch.