I'm implementing a hash table that is supposed to store pairs of 32-bit values. Considering my elements are fixed size, I'm using a very simple hashing function:
hash(a,b) = asUint64(a) + (asUint64(b) << 32)
With that, I get the index of an element in a hash table (that is, its corresponding bucket) with:
index(a,b) = hash(a,b) % hash_size
Where hash_size is the number of entries/buckets on my table. I've realized, though, that I could speed up this implementation a little bit if I replaced the "modulus" operator by a bitwise mod of 2
, by fixing hash_size as a power of 2. Except, when I do that, most of my pairs end up on the first bucket! Why is that happening?