Answers.Body: <p>I found some well hidden pages in the Chess Programming Wiki.</p>
  <p>The first one basically introduces your solution:</p>
  <p><a href="https://www.chessprogramming.org/Bitboard_Serialization#Single_Bits" rel="nofollow noreferrer">https://www.chessprogramming.org/Bitboard_Serialization#Single_Bits</a></p>
  <p>However, the next chapter states that most applications are interested in the index of the square rather than its bitboard representation:</p>
  <p><a href="https://www.chessprogramming.org/Bitboard_Serialization#Square_Index_Serialization" rel="nofollow noreferrer">https://www.chessprogramming.org/Bitboard_Serialization#Square_Index_Serialization</a></p>
  <p>This is done using a <strong>bitscan</strong> routine. There is another page dedicated to them:</p>
  <p><a href="https://www.chessprogramming.org/BitScan" rel="nofollow noreferrer">https://www.chessprogramming.org/BitScan</a></p>
  <p>It shouldn't be a surprise that the fastest method is to use processor specific instructions:</p>
  <p><a href="https://www.chessprogramming.org/BitScan#bsfbsr" rel="nofollow noreferrer">https://www.chessprogramming.org/BitScan#bsfbsr</a></p>
  <p>If this is not possible, there are many other algorithms described there, which you can choose from.</p>
  <p>There is also a Java dedicated version of this page, which describes API methods, which <a href="https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/jdk/internal/HotSpotIntrinsicCandidate.java" rel="nofollow noreferrer">may be intrinsified by the VM</a>:</p>
  <p><a href="https://www.chessprogramming.org/Java-Bitscan" rel="nofollow noreferrer">https://www.chessprogramming.org/Java-Bitscan</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-14T18:53:49.763Z
Answers.LastActivityDate: 2021-04-14T18:53:49.763Z
Answers.UserDisplayName: Sleafar
Body: <p>I am making a chess engine, and for now I’m working on the move generation. I’m using bitboards, and so for example my magic bit boards file for a knight on d4 is</p>
  
  00000000<br>
  00000000<br>
  00101000<br>
  01000100<br>
  000N0000<br>
  01000100<br>
  00101000<br>
  00000000<br><br>
  <p>Which is represented by some unsigned long long integer.
  
  <p>I need this in a format where I can store the moves in a move list, and also make the move. E.g. {10,12,17,21,33,37,42,44}</p>
  <p>What would be the fastest way to get the indices of the “1” bits in the bitboard during runtime? Currently I isolate the least significant 1 bit with x &amp;= -x, and then XOR the position until I get 0. But is there a faster way to do this?</p>
  
Tags: <engines><bitboard>
Title: Fastest way to parse a bitboard
ThreadText: # Fastest way to parse a bitboard
  
  (Asked by: username_0 on 2021-04-12)
  
  I am making a chess engine, and for now I’m working on the move generation. I’m using bitboards, and so for example my magic bit boards file for a knight on d4 is
  
  00000000  
  00000000  
  00101000  
  01000100  
  000N0000  
  01000100  
  00101000  
  00000000  
    
  
  Which is represented by some unsigned long long integer.
  
  I need this in a format where I can store the moves in a move list, and also make the move. E.g. {10,12,17,21,33,37,42,44}
  
  What would be the fastest way to get the indices of the “1” bits in the bitboard during runtime? Currently I isolate the least significant 1 bit with x &= -x, and then XOR the position until I get 0. But is there a faster way to do this?
  
  **Tags:** engines, bitboard
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-14. Score: 4 | Accepted answer
  
  I found some well hidden pages in the Chess Programming Wiki.
  
  The first one basically introduces your solution:
  
  <https://www.chessprogramming.org/Bitboard_Serialization#Single_Bits>
  
  However, the next chapter states that most applications are interested in the index of the square rather than its bitboard representation:
  
  <https://www.chessprogramming.org/Bitboard_Serialization#Square_Index_Serialization>
  
  This is done using a **bitscan** routine. There is another page dedicated to them:
  
  <https://www.chessprogramming.org/BitScan>
  
  It shouldn't be a surprise that the fastest method is to use processor specific instructions:
  
  <https://www.chessprogramming.org/BitScan#bsfbsr>
  
  If this is not possible, there are many other algorithms described there, which you can choose from.
  
  There is also a Java dedicated version of this page, which describes API methods, which [may be intrinsified by the VM](https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/jdk/internal/HotSpotIntrinsicCandidate.java):
  
  <https://www.chessprogramming.org/Java-Bitscan>
  
  
  ---
  
  