Why multiply by a prime before xoring in many GetHashCode Implementations?

前端 未结 4 2125
既然无缘
既然无缘 2021-01-06 10:33

I understand that multiplication by a large number before xoring should help with badly distributed operands but why should the multiplier be a prime?

4条回答
  •  太阳男子
    2021-01-06 11:19

    Multiplying by a non-prime has a cyclic repeating pattern much smaller than the number. If you use a prime then the cyclic repeating pattern is guaranteeed to be at least as large as the prime number.

提交回复
热议问题