Answers.Body: <p>What programming language do you want to use?</p>
  <p>To implement a bitboard in C#, use <a href="https://learn.microsoft.com/en-us/dotnet/api/system.uint64?view=net-7.0" rel="nofollow noreferrer">System.UInt64</a>. This can hold 64 bits, 1 for each square of the chess board. This value type lends itself to many fast bitwise operations.</p>
  <p>This is a <a href="https://www.chessprogramming.org/Bitboards" rel="nofollow noreferrer">good bitboard tutorial</a>.</p>
  <p>Here are some examples from my own C# chess engine. As you can see from the code, it can take a while to wrap your head around using bitboards, but they are typically very fast, especially for position evaluation.</p>
  <p>Example 1 - Bitboard definition:</p>
  <pre><code>internal UInt64 WhiteKing;
  internal UInt64 WhiteQueens;
  internal UInt64 WhiteRooks;
  internal UInt64 WhiteBishops;
  internal UInt64 WhiteKnights;
  internal UInt64 WhitePawns;
  internal UInt64 WhitePieces;
  </code></pre>
  <p>Example 2 - Bitboard initialisation:</p>
  <pre><code>// Initialise piece bitboards using square contents.
  private void InitPieceBitboards()
  {
      this.WhiteKing = 0; 
      this.WhiteQueens = 0; 
      this.WhiteRooks = 0; 
      this.WhiteBishops = 0; 
      this.WhiteKnights = 0; 
      this.WhitePawns = 0;
  
      for (Int16 i = 0; i &lt; 64; i++)
      {
          if (this.Squares[i] == Constants.WHITE_KING)
          {
              this.WhiteKing = this.WhiteKing | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_QUEEN)
          {
              this.WhiteQueens = this.WhiteQueens | Constants.BITSET[i];
          } 
          if (this.Squares[i] == Constants.WHITE_ROOK) 
          {
              this.WhiteRooks = this.WhiteRooks | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_BISHOP) 
          {
              this.WhiteBishops = this.WhiteBishops | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_KNIGHT) 
          {
              this.WhiteKnights = this.WhiteKnights | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_PAWN) 
          {
              this.WhitePawns = this.WhitePawns | Constants.BITSET[i];
          }
  
          this.WhitePieces = this.WhiteKing | this.WhiteQueens | 
                             this.WhiteRooks | this.WhiteBishops | 
                             this.WhiteKnights | this.WhitePawns;
          this.BlackPieces = this.BlackKing | this.BlackQueens | 
                             this.BlackRooks | this.BlackBishops | 
                             this.BlackKnights | this.BlackPawns;
          this.SquaresOccupied = this.WhitePieces | this.BlackPieces;
      }
  }
  </code></pre>
  <p>Example 3 - Move generation:</p>
  <pre><code>// We can't capture one of our own pieces.
  eligibleSquares = ~this.WhitePieces;
  
  // Generate moves for white knights.
  remainingKnights = this.WhiteKnights;
  
  // Generate the moves for each knight...
  while (remainingKnights != 0)
  {
      squareFrom = BitOps.BitScanForward(remainingKnights);
      generatedMoves = Constants.ATTACKS_KNIGHT[squareFrom] &amp; eligibleSquares;
      while (generatedMoves != 0)
      {
          squareTo = BitOps.BitScanForward(generatedMoves);
          moveList.Add(new Move(squareFrom, squareTo, Constants.WHITE_KNIGHT, 
                                this.Squares[squareTo], Constants.EMPTY));
          generatedMoves ^= Constants.BITSET[squareTo];
      }
      // Finished with this knight - move on to the next one.
      remainingKnights ^= Constants.BITSET[squareFrom];
  }    
  </code></pre>
  <p>Example 4 - Calculate material score:</p>
  <pre><code>// Material score from scratch, in centipawns from White's perspective.
  internal static Int32 ScoreMaterial(Board position)
  {
      return BitOps.BitCountWegner(position.WhitePawns)   * Constants.VALUE_PAWN +
             BitOps.BitCountWegner(position.WhiteKnights) * Constants.VALUE_KNIGHT +
             BitOps.BitCountWegner(position.WhiteBishops) * Constants.VALUE_BISHOP +
             BitOps.BitCountWegner(position.WhiteRooks)   * Constants.VALUE_ROOK   +
             BitOps.BitCountWegner(position.WhiteQueens)  * Constants.VALUE_QUEEN  -
             BitOps.BitCountWegner(position.BlackPawns)   * Constants.VALUE_PAWN   -
             BitOps.BitCountWegner(position.BlackKnights) * Constants.VALUE_KNIGHT -
             BitOps.BitCountWegner(position.BlackBishops) * Constants.VALUE_BISHOP -
             BitOps.BitCountWegner(position.BlackRooks)   * Constants.VALUE_ROOK   -
             BitOps.BitCountWegner(position.BlackQueens)  * Constants.VALUE_QUEEN;
  }
  </code></pre>
  <p>Example 5 - Calculating piece mobility:</p>
  <pre><code>// Calculate mobility score for white knights.
  remainingPieces = position.WhiteKnights;
  while (remainingPieces != 0)
  {
      squareFrom = BitOps.BitScanForward(remainingPieces);
      mobilityKnight += BitOps.BitCountWegner(Constants.ATTACKS_KNIGHT[squareFrom]
                                              &amp; unoccupiedSquares);
      remainingPieces ^= Constants.BITSET[squareFrom];
   }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-11T20:54:54.860Z
Answers.Text: can you provide the definition of moveList?
Answers.UserDisplayName: Carlos
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2013-09-16T22:31:43.253Z
Answers.LastActivityDate: 2022-09-24T13:05:23.113Z
Answers.LastEditDate: 2022-09-24T13:05:23.113Z
Answers.UserDisplayName: HTTP 410
Answers.Body: <p>The best resource for chess engine programming is the <a href="https://www.chessprogramming.org/Main_Page" rel="noreferrer">Chess Programming Wiki</a>, which has a large <a href="https://www.chessprogramming.org/Bitboards" rel="noreferrer">section on bitboards</a>. Everything you need to make a bitboard-based engine is there, although it's rather spread out and sometimes written by people for whom English is a second language.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2013-09-12T12:17:18.057Z
Answers.LastActivityDate: 2018-11-14T07:13:03.737Z
Answers.LastEditDate: 2018-11-14T07:13:03.737Z
Answers.UserDisplayName: dfan
Body: <p>I'm interested in programming a chess engine and using bitboards to represent the state of the game. I know there are a few open source chess engines that use bitboards, but its not so easy to look at the code and understand what is going on. I'm looking for good reference material on how to represent all the state in bitboards.</p>
  
  <p>Clearly explaining how to maintain the state of the game using bitboards, and especially how to generate a list of valid moves from any given bitboard, or supplying good references to such an explanation will earn you the green check mark.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-08T20:29:46.067Z
Comments.Text: Stating facts is not being rude or sour, @AryanParekh
Comments.UserDisplayName: Tony Ennis
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-30T21:45:35.233Z
Comments.Text: @TonyEnnis People like you are what make stack exchange so toxic.
Comments.UserDisplayName: Marlon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-08-21T00:25:49.433Z
Comments.Text: @TonyEnnis  programming specific to chess engines is explicitly on-topic.
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-09-12T12:13:50.453Z
Comments.Text: 1. OP shows no knowledge of the topic. That is, the OP has not tried seriously to educate himself.
  2. This is about programming, not chess
Comments.UserDisplayName: Tony Ennis
Tags: <engines><software><open-source><bitboard>
Title: How to represent chess state with a bitboard
ThreadText: # How to represent chess state with a bitboard
  
  (Asked by: username_0 on 2013-09-12)
  
  I'm interested in programming a chess engine and using bitboards to represent the state of the game. I know there are a few open source chess engines that use bitboards, but its not so easy to look at the code and understand what is going on. I'm looking for good reference material on how to represent all the state in bitboards.
  
  Clearly explaining how to maintain the state of the game using bitboards, and especially how to generate a list of valid moves from any given bitboard, or supplying good references to such an explanation will earn you the green check mark.
  
  **Tags:** engines, software, open-source, bitboard
  
  > **Comments:**
  > **username_1**: 1. OP shows no knowledge of the topic. That is, the OP has not tried seriously to educate himself. 2. This is about programming, not chess  
  > **username_1**: Stating facts is not being rude or sour, @AryanParekh  
  > **username_2**: @TonyEnnis People like you are what make stack exchange so toxic.  
  > **username_3**: @TonyEnnis  programming specific to chess engines is explicitly on-topic.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_6 on 2013-09-12. Score: 14 | Accepted answer
  
  The best resource for chess engine programming is the [Chess Programming Wiki](https://www.chessprogramming.org/Main_Page), which has a large [section on bitboards](https://www.chessprogramming.org/Bitboards). Everything you need to make a bitboard-based engine is there, although it's rather spread out and sometimes written by people for whom English is a second language.
  
  
  ---
  
  ### Answer by username_4 on 2013-09-16. Score: 11
  
  What programming language do you want to use?
  To implement a bitboard in C#, use System.UInt64. This can hold 64 bits, 1 for each square of the chess board. This value type lends itself to many fast bitwise operations.
  This is a good bitboard tutorial.
  Here are some examples from my own C# chess engine. As you can see from the code, it can take a while to wrap your head around using bitboards, but they are typically very fast, especially for position evaluation.
  Example 1 - Bitboard definition:
  internal UInt64 WhiteKing;
  internal UInt64 WhiteQueens;
  internal UInt64 WhiteRooks;
  internal UInt64 WhiteBishops;
  internal UInt64 WhiteKnights;
  internal UInt64 WhitePawns;
  internal UInt64 WhitePieces;
  
  Example 2 - Bitboard initialisation:
  // Initialise piece bitboards using square contents.
  private void InitPieceBitboards()
  {
      this.WhiteKing = 0; 
      this.WhiteQueens = 0; 
      this.WhiteRooks = 0; 
      this.WhiteBishops = 0; 
      this.WhiteKnights = 0; 
      this.WhitePawns = 0;
  
      for (Int16 i = 0; i < 64; i++)
      {
          if (this.Squares[i] == Constants.WHITE_KING)
          {
              this.WhiteKing = this.WhiteKing | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_QUEEN)
          {
              this.WhiteQueens = this.WhiteQueens | Constants.BITSET[i];
          } 
          if (this.Squares[i] == Constants.WHITE_ROOK) 
          {
              this.WhiteRooks = this.WhiteRooks | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_BISHOP) 
          {
              this.WhiteBishops = this.WhiteBishops | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_KNIGHT) 
          {
              this.WhiteKnights = this.WhiteKnights | Constants.BITSET[i];
          }
          if (this.Squares[i] == Constants.WHITE_PAWN) 
          {
              this.WhitePawns = this.WhitePawns | Constants.BITSET[i];
          }
  
          this.WhitePieces = this.WhiteKing | this.WhiteQueens | 
                             this.WhiteRooks | this.WhiteBishops | 
                             this.WhiteKnights | this.WhitePawns;
          this.BlackPieces = this.BlackKing | this.BlackQueens | 
                             this.BlackRooks | this.BlackBishops | 
                             this.BlackKnights | this.BlackPawns;
          this.SquaresOccupied = this.WhitePieces | this.BlackPieces;
      }
  }
  
  Example 3 - Move generation:
  // We can't capture one of our own pieces.
  eligibleSquares = ~this.WhitePieces;
  
  // Generate moves for white knights.
  remainingKnights = this.WhiteKnights;
  
  // Generate the moves for each knight...
  while (remainingKnights != 0)
  {
      squareFrom = BitOps.BitScanForward(remainingKnights);
      generatedMoves = Constants.ATTACKS_KNIGHT[squareFrom] & eligibleSquares;
      while (generatedMoves != 0)
      {
          squareTo = BitOps.BitScanForward(generatedMoves);
          moveList.Add(new Move(squareFrom, squareTo, Constants.WHITE_KNIGHT, 
                                this.Squares[squareTo], Constants.EMPTY));
          generatedMoves ^= Constants.BITSET[squareTo];
      }
      // Finished with this knight - move on to the next one.
      remainingKnights ^= Constants.BITSET[squareFrom];
  }    
  
  Example 4 - Calculate material score:
  // Material score from scratch, in centipawns from White's perspective.
  internal static Int32 ScoreMaterial(Board position)
  {
      return BitOps.BitCountWegner(position.WhitePawns)   * Constants.VALUE_PAWN +
             BitOps.BitCountWegner(position.WhiteKnights) * Constants.VALUE_KNIGHT +
             BitOps.BitCountWegner(position.WhiteBishops) * Constants.VALUE_BISHOP +
             BitOps.BitCountWegner(position.WhiteRooks)   * Constants.VALUE_ROOK   +
             BitOps.BitCountWegner(position.WhiteQueens)  * Constants.VALUE_QUEEN  -
             BitOps.BitCountWegner(position.BlackPawns)   * Constants.VALUE_PAWN   -
             BitOps.BitCountWegner(position.BlackKnights) * Constants.VALUE_KNIGHT -
             BitOps.BitCountWegner(position.BlackBishops) * Constants.VALUE_BISHOP -
             BitOps.BitCountWegner(position.BlackRooks)   * Constants.VALUE_ROOK   -
             BitOps.BitCountWegner(position.BlackQueens)  * Constants.VALUE_QUEEN;
  }
  
  Example 5 - Calculating piece mobility:
  // Calculate mobility score for white knights.
  remainingPieces = position.WhiteKnights;
  while (remainingPieces != 0)
  {
      squareFrom = BitOps.BitScanForward(remainingPieces);
      mobilityKnight += BitOps.BitCountWegner(Constants.ATTACKS_KNIGHT[squareFrom]
                                              & unoccupiedSquares);
      remainingPieces ^= Constants.BITSET[squareFrom];
   }
  
  > **Comments:**
  > **username_5**: can you provide the definition of moveList?  
  
  ---
  
  