Body: <p>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.</p>
  <p>According to the Chess Programming Wiki, since the 64-bit zobrist is too large to use as the index itself, &quot;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.&quot;</p>
  <p>My current implementation looks like this:</p>
  <pre><code>private int getIndex(long zobristKey) {
      return (int) Math.abs(zobristKey % TABLE_SIZE);
  }
  </code></pre>
  <p>I need to use Math.abs, because otherwise half of the resulting numbers are negative, and therefore not suitable as an index.</p>
  <p>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.</p>
  <p>Does anyone spot an error in my implementation, or else can fill the gap in my understanding?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-16T12:51:07.463Z
Comments.Text: The index calculation itself looks fine to me. But indeed, more information is needed. What is the table size, how many entries do you put in the table? How exactly do you calculate your Zobrist keys?
Comments.UserDisplayName: koedem
Tags: <engines><programming><transpositions>
Title: How to calculate transposition table hash index
ThreadText: # How to calculate transposition table hash index
  
  (Asked by: username_0 on 2023-11-15)
  
  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?
  
  **Tags:** engines, programming, transpositions
  
  > **Comments:**
  > **username_1**: The index calculation itself looks fine to me. But indeed, more information is needed. What is the table size, how many entries do you put in the table? How exactly do you calculate your Zobrist keys?  
  
  ---
  
  ## 0 answers
  
  