I'm writing a chess engine in Java, and I'm having difficulty understanding how to calculate the hash address/index, based on the zobrist key.
According to the Chess Programming Wiki, since the 64-bit zobrist is too large to use as the index itself, "to calculate the address or index requires signature modulo number of entries, for power of two sized tables, the lower part of the hash key, masked by an 'and'-instruction accordantly."
My current implementation looks like this:
private int getIndex(long zobristKey) {
return (int) Math.abs(zobristKey % TABLE_SIZE);
}
I need to use Math.abs, because otherwise half of the resulting numbers are negative, and therefore not suitable as an index.
The problem I have is that this current implementation generates thousands of collisions per search, and I can't work out why. It might be because the value is not 'masked by an and-instruction' as the CPW says, but I'm not sure what value I am supposed to be and-ing my key with.
Does anyone spot an error in my implementation, or else can fill the gap in my understanding?