# Hash table: When is it suitable to use modulo power of 2?

I am implementing a hash table that may need to grow and shrink as the number of stored keys varies. I have a hashcode function that uniformly hashes keys to positive 32-bit integers. The table itself will use a smaller array of approximate size M.

If I wanted to use the hash function to produce a value between 0 and P-1 (if P is close to M), can I use mod P if P is a power of 2?

What about using mod P, where P is a prime?

• I don't understand your question. Why are you reducing your hash value modulo anything other than the size of the table? If you do that, the top elements of the table ($P\dots M-1$) will never be used. Sep 22 '17 at 11:22
• Possible duplicate of Why is it best to use a prime number as a mod in a hashing function? Sep 22 '17 at 12:11