Answers.Body: <p>To convert a single bitboard to a numpy array:</p>
  <pre><code>def bitboard_to_array(bb: int) -&gt; np.ndarray:
      s = 8 * np.arange(7, -1, -1, dtype=np.uint64)
      b = (bb &gt;&gt; s).astype(np.uint8)
      b = np.unpackbits(b, bitorder=&quot;little&quot;)
      return b.reshape(8, 8)
  </code></pre>
  <p>To convert multiple bitboards to a numpy array:</p>
  <pre><code>def bitboards_to_array(bb: np.ndarray) -&gt; np.ndarray:
      bb = np.asarray(bb, dtype=np.uint64)[:, np.newaxis]
      s = 8 * np.arange(7, -1, -1, dtype=np.uint64)
      b = (bb &gt;&gt; s).astype(np.uint8)
      b = np.unpackbits(b, bitorder=&quot;little&quot;)
      return b.reshape(-1, 8, 8)
  </code></pre>
  <p>Example usage:</p>
  <pre><code>import chess
  import numpy as np
  
  board = chess.Board()
  
  black, white = board.occupied_co
  
  bitboards = np.array([
      black &amp; board.pawns,
      black &amp; board.knights,
      black &amp; board.bishops,
      black &amp; board.rooks,
      black &amp; board.queens,
      black &amp; board.kings,
      white &amp; board.pawns,
      white &amp; board.knights,
      white &amp; board.bishops,
      white &amp; board.rooks,
      white &amp; board.queens,
      white &amp; board.kings,
  ], dtype=np.uint64)
  
  board_array = bitboards_to_array(bitboards)
  </code></pre>
  <p>Which gives:</p>
  <pre><code>&gt;&gt;&gt; board_array.shape
  (12, 8, 8)
  
  &gt;&gt;&gt; black_pawns = board_array[0]
  ... print(black_pawns)
  ... 
  [[0 0 0 0 0 0 0 0]
   [1 1 1 1 1 1 1 1]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]]
  
  &gt;&gt;&gt; black_kings = board_array[5]
  ... print(black_kings)
  ... 
  [[0 0 0 0 1 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-07T05:00:31.260Z
Answers.LastActivityDate: 2021-03-07T05:20:59.507Z
Answers.LastEditDate: 2021-03-07T05:20:59.507Z
Answers.UserDisplayName: Mateen Ulhaq
Answers.Body: <p>python-chess already has the built in utilities to handle conversions. Here is an example:</p>
  
  <pre><code>import chess
  
  board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
  
  WP = board.pieces(chess.PAWN, chess.WHITE)
  BP = board.pieces(chess.PAWN, chess.BLACK)
  
  print (int(WP), int (BP))
  
  print (list(WP), list(BP))
  
  print ("\nWHITE PAWNS:\n" + str(WP))
  
  print ("\nBLACK PAWNS:\n" + str(BP))
  </code></pre>
  
  <p>Output:    </p>
  
  <pre><code>65280 71776119061217280
  
  [8, 9, 10, 11, 12, 13, 14, 15] [48, 49, 50, 51, 52, 53, 54, 55]
  
  WHITE PAWNS:
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  1 1 1 1 1 1 1 1
  . . . . . . . .
  
  BLACK PAWNS:
  . . . . . . . .
  1 1 1 1 1 1 1 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  </code></pre>
  
  <p>From a list, or from an integer, you can easily construct a Bitboard however you choose to define it. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-14T13:11:15.900Z
Answers.LastActivityDate: 2020-05-14T13:11:15.900Z
Answers.UserDisplayName: AndrewGrant
Body: <p>I would like to convert a given chess board into its bitboard representation, but my implementation is rather slow. Here is what I'm doing (using the python-chess package):</p>
  
  <pre><code>board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1)
  
  w_pawn = (np.asarray(board.pieces(chess.PAWN, chess.WHITE).tolist())).astype(int)
  w_rook = (np.asarray(board.pieces(chess.ROOK, chess.WHITE).tolist())).astype(int)
  w_knight = (np.asarray(board.pieces(chess.KNIGHT, chess.WHITE).tolist())).astype(int)
  w_bishop = (np.asarray(board.pieces(chess.BISHOP, chess.WHITE).tolist())).astype(int)
  w_queen = (np.asarray(board.pieces(chess.QUEEN, chess.WHITE).tolist())).astype(int)
  w_king = (np.asarray(board.pieces(chess.KING, chess.WHITE).tolist())).astype(int)
  
  b_pawn = (np.asarray(board.pieces(chess.PAWN, chess.BLACK).tolist())).astype(int)
  b_rook = (np.asarray(board.pieces(chess.ROOK, chess.BLACK).tolist())).astype(int)
  b_knight = (np.asarray(board.pieces(chess.KNIGHT, chess.BLACK).tolist())).astype(int)
  b_bishop = (np.asarray(board.pieces(chess.BISHOP, chess.BLACK).tolist())).astype(int)
  b_queen = (np.asarray(board.pieces(chess.QUEEN, chess.BLACK).tolist())).astype(int)
  b_king = (np.asarray(board.pieces(chess.KING, chess.BLACK).tolist())).astype(int)
  
  return np.concatenate((w_pawn, w_rook, w_knight, w_bishop, w_queen, w_king,
                         b_pawn, b_rook, b_knight, b_bishop, b_queen, b_king))
  </code></pre>
  
  <p>For the starting position this function returns:</p>
  
  <pre><code>[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0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0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0]
  </code></pre>
  
  <p>Which is what I want. The only issue is the speed of this function.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-24T11:17:06.307Z
Comments.Text: I've seen many chess libraries written in different languages, but none of them use bitboard representation.
Comments.UserDisplayName: Mike Jones
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-03-07T05:34:10.333Z
Comments.Text: GNU-Chess, aka Crafty, uses bitboards. I could be wrong; always allow for that!
Comments.UserDisplayName: hsmyers
Tags: <engines><programming><python><performance><bitboard>
Title: Quickly converting board to bitboard representation using python-chess library
ThreadText: # Quickly converting board to bitboard representation using python-chess library
  
  (Asked by: username_0 on 2020-04-22)
  
  I would like to convert a given chess board into its bitboard representation, but my implementation is rather slow. Here is what I'm doing (using the python-chess package):
  
  ```
  board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1)
  
  w_pawn = (np.asarray(board.pieces(chess.PAWN, chess.WHITE).tolist())).astype(int)
  w_rook = (np.asarray(board.pieces(chess.ROOK, chess.WHITE).tolist())).astype(int)
  w_knight = (np.asarray(board.pieces(chess.KNIGHT, chess.WHITE).tolist())).astype(int)
  w_bishop = (np.asarray(board.pieces(chess.BISHOP, chess.WHITE).tolist())).astype(int)
  w_queen = (np.asarray(board.pieces(chess.QUEEN, chess.WHITE).tolist())).astype(int)
  w_king = (np.asarray(board.pieces(chess.KING, chess.WHITE).tolist())).astype(int)
  
  b_pawn = (np.asarray(board.pieces(chess.PAWN, chess.BLACK).tolist())).astype(int)
  b_rook = (np.asarray(board.pieces(chess.ROOK, chess.BLACK).tolist())).astype(int)
  b_knight = (np.asarray(board.pieces(chess.KNIGHT, chess.BLACK).tolist())).astype(int)
  b_bishop = (np.asarray(board.pieces(chess.BISHOP, chess.BLACK).tolist())).astype(int)
  b_queen = (np.asarray(board.pieces(chess.QUEEN, chess.BLACK).tolist())).astype(int)
  b_king = (np.asarray(board.pieces(chess.KING, chess.BLACK).tolist())).astype(int)
  
  return np.concatenate((w_pawn, w_rook, w_knight, w_bishop, w_queen, w_king,
                         b_pawn, b_rook, b_knight, b_bishop, b_queen, b_king))
  
  ```
  
  For the starting position this function returns:
  
  ```
  [0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0]
  
  ```
  
  Which is what I want. The only issue is the speed of this function.
  
  **Tags:** engines, programming, python, performance, bitboard
  
  > **Comments:**
  > **username_1**: I've seen many chess libraries written in different languages, but none of them use bitboard representation.  
  > **username_2**: GNU-Chess, aka Crafty, uses bitboards. I could be wrong; always allow for that!  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2021-03-07. Score: 6
  
  To convert a single bitboard to a numpy array:
  
  ```
  def bitboard_to_array(bb: int) -> np.ndarray:
      s = 8 * np.arange(7, -1, -1, dtype=np.uint64)
      b = (bb >> s).astype(np.uint8)
      b = np.unpackbits(b, bitorder="little")
      return b.reshape(8, 8)
  
  ```
  
  To convert multiple bitboards to a numpy array:
  
  ```
  def bitboards_to_array(bb: np.ndarray) -> np.ndarray:
      bb = np.asarray(bb, dtype=np.uint64)[:, np.newaxis]
      s = 8 * np.arange(7, -1, -1, dtype=np.uint64)
      b = (bb >> s).astype(np.uint8)
      b = np.unpackbits(b, bitorder="little")
      return b.reshape(-1, 8, 8)
  
  ```
  
  Example usage:
  
  ```
  import chess
  import numpy as np
  
  board = chess.Board()
  
  black, white = board.occupied_co
  
  bitboards = np.array([
      black & board.pawns,
      black & board.knights,
      black & board.bishops,
      black & board.rooks,
      black & board.queens,
      black & board.kings,
      white & board.pawns,
      white & board.knights,
      white & board.bishops,
      white & board.rooks,
      white & board.queens,
      white & board.kings,
  ], dtype=np.uint64)
  
  board_array = bitboards_to_array(bitboards)
  
  ```
  
  Which gives:
  
  ```
  >>> board_array.shape
  (12, 8, 8)
  
  >>> black_pawns = board_array[0]
  ... print(black_pawns)
  ... 
  [[0 0 0 0 0 0 0 0]
   [1 1 1 1 1 1 1 1]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]]
  
  >>> black_kings = board_array[5]
  ... print(black_kings)
  ... 
  [[0 0 0 0 1 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]
   [0 0 0 0 0 0 0 0]]
  
  ```
  
  
  ---
  
  ### Answer by username_4 on 2020-05-14. Score: 6
  
  python-chess already has the built in utilities to handle conversions. Here is an example:
  
  ```
  import chess
  
  board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
  
  WP = board.pieces(chess.PAWN, chess.WHITE)
  BP = board.pieces(chess.PAWN, chess.BLACK)
  
  print (int(WP), int (BP))
  
  print (list(WP), list(BP))
  
  print ("\nWHITE PAWNS:\n" + str(WP))
  
  print ("\nBLACK PAWNS:\n" + str(BP))
  
  ```
  
  Output:
  
  ```
  65280 71776119061217280
  
  [8, 9, 10, 11, 12, 13, 14, 15] [48, 49, 50, 51, 52, 53, 54, 55]
  
  WHITE PAWNS:
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  1 1 1 1 1 1 1 1
  . . . . . . . .
  
  BLACK PAWNS:
  . . . . . . . .
  1 1 1 1 1 1 1 1
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  . . . . . . . .
  
  ```
  
  From a list, or from an integer, you can easily construct a Bitboard however you choose to define it.
  
  
  ---
  
  