I want to use a pairwise independent hashing to implement an algorithm.
According to this answer on Obtaining a k-wise independent hash function, it seems that it is enough to compute (a*x + b) % p % m
for mapping an integer x
(which is smaller than p
) to {0,1,...,m-1}.
Then I saw the following open source implementation: . That seems to implement the above algorithm for p=2^31-1.
Can anyone help me make sense of it?
It seems that by doing & MOD
, the code is actually computing (modulo 2^31) and not (modulo 2^31-1)?
What does line 113 (seen on the screenshot) actually do? Why would you shift result and add it again?