math - Why using powers of 2 as the hash size makes a hash table considerably worse than using primes? -



math - Why using powers of 2 as the hash size makes a hash table considerably worse than using primes? -

i'm implementing hash table supposed store pairs of 32-bit values. considering elements fixed size, i'm using simple hashing function:

hash(a,b) = asuint64(a) + (asuint64(b) << 32)

with that, index of element in hash table (that is, corresponding bucket) with:

index(a,b) = hash(a,b) % hash_size

where hash_size number of entries/buckets on table. i've realized, though, speed implementation little bit if replaced "modulus" operator bitwise mod of 2, fixing hash_size powerfulness of 2. except, when that, of pairs end on first bucket! why happening?

my guess info not evenly distributed in a. consider concatenation of a , b hash code:

b31b30...b1b0a31a30...a1a0, ai, bi ith bit of a,b

suppose have table 1000000 entries, hash index

a9a8...a1a0 (as integer)

worse, suppose a ever ranges 1 100. have less dependence on higher order bits of a.

as can see, if hash table doesn't have @ to the lowest degree 4 billion entries, hashcode has no dependence on b @ all, , hash(x, a) collide x.

math hash language-agnostic hashtable bit-manipulation

Comments

Popular posts from this blog

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -