Physical Location Bitboards

Real Chess Game State and Bitboards

It might not seem very obvious how exactly you use a bitboard, which only has bits that can be either on or off, to represent a chess board with all of the pieces on it. Well, as alluded to in the last page, you can't. You actually need a minimum of 12 bitboards to represent the positions of each kind and color of piece on a chessboard. These bitboards are named: WhitePawns, WhiteRooks, WhiteKnights, WhiteBishops, WhiteQueens, WhiteKing, BlackPawns, BlackRooks, BlackKnights, BlackBishops, BlackQueens, and BlackKing. Each bitboard represents one small slice of the position state of the board and together form the big picture of a board state. So, what do the 12 initial position bitboards look like both in the chess board visualization and the bitboard visualization?

WhitePawns

The initial physical location bitboard for ALL of the white pawns is this:
               
               
               
               
               
               
P P P P P P P P
               
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
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
WhitePawns:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

WhiteRooks

The initial physical location bitboard for ALL of the white rooks is this:
               
               
               
               
               
               
               
R             R
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
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
WhiteRooks:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

WhiteKnights

The initial physical location bitboard for ALL of the white knights is this:
               
               
               
               
               
               
               
  N         N  
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 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
WhiteKnights:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

WhiteBishops

The initial physical location bitboard for ALL of the white bishops is this:
               
               
               
               
               
               
               
    B     B    
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 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
WhiteBishops:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

WhiteQueens

The initial physical location bitboard for ALL of the white queens(initially there is only one, but through pawn promotion more can show up) is this:
               
               
               
               
               
               
               
      Q        
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 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
WhiteQueens:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

WhiteKing

The initial physical location bitboard for the white king is this:
               
               
               
               
               
               
               
        K      
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 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
WhiteKing:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

BlackPawns

The initial physical location bitboard for ALL of the black pawns is this:
               
p p p p p p p p
               
               
               
               
               
               
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
BlackPawns:
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

BlackRooks

The initial physical location bitboard for ALL of the black rooks is this:
r             r
               
               
               
               
               
               
               
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
BlackRooks:
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

BlackKnights

The initial physical location bitboard for ALL of the black knights is this:
  n         n  
               
               
               
               
               
               
               
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
BlackKnights:
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

BlackBishops

The initial physical location bitboard for ALL of the black bishops is this:
    b     b    
               
               
               
               
               
               
               
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
BlackBishops:
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

BlackQueens

The initial physical location bitboard for ALL of the black queens(initially there is only one, but through pawn promotion more can show up) is this:
      q        
               
               
               
               
               
               
               
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
BlackQueens:
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

BlackKing

The initial physical location bitboard for the black king is this:
        k      
               
               
               
               
               
               
               
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
BlackKing:
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

Commonly Needed Extra Physical Location Bitboards

The twelve above are the minimum needed to represent every piece on the board. However, there are three very common bitboards that are so useful, I'm giving them special mention: AllWhitePieces, AllBlackPieces, and AllPieces. These boards can be derived from the other 12 boards.

Here is the derivation of AllWhitePieces.

AllWhitePieces = WhitePawns | WhiteRooks | WhiteKnights | WhiteBishops | 
					WhiteQueens | WhiteKing;
which produces a board that looks like this:
               
               
               
               
               
               
P P P P P P P P
R N B Q K B N R
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
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
1 1 1 1 1 1 1 1
AllWhitePieces:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 1 1 1 1 1 1 1 1

AllBlackPieces can be derived in the same manner:

AllBlackPieces = BlackPawns | BlackRooks | BlackKnights | BlackBishops | 
					BlackQueens | BlackKing;
which produces a board that looks like this:
r n b q k b n r
p p p p p p p p
               
               
               
               
               
               
1 1 1 1 1 1 1 1
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
AllWhitePieces:
1 1 1 1 1 1 1 1 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

And finally, AllPieces can be created simply by ORing the AllWhitePieces and AllBlackPieces together:

AllPieces = AllWhitePieces | AllBlackPieces;
which produces a board that looks like this:
r n b q k b n r
p p p p p p p p
               
               
               
               
P P P P P P P P
R N B Q K B N R
1 1 1 1 1 1 1 1
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
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
AllPieces:
1 1 1 1 1 1 1 1 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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

It turns out that these boards are so very useful, that instead of deriving them like I did above, they are often kept manually updated by the program itself.

The Chess Board

Now we have enough information to create an actual definition for a chess board via bitboards. It is quite simple, really:

struct ChessBoard
{
	/* The white piece positions */
	Bitboard WhitePawns;
	Bitboard WhiteRooks;
	Bitboard WhiteKnights;
	Bitboard WhiteBishops;
	Bitboard WhiteQueens;
	Bitboard WhiteKing;

	/* The black piece positions */
	Bitboard BlackPawns;
	Bitboard BlackRooks;
	Bitboard BlackKnights;
	Bitboard BlackBishops;
	Bitboard BlackQueens;
	Bitboard BlackKing;

	/* Commonly derived positions */
	Bitboard AllWhitePieces;
	Bitboard AllBlackPieces;
	Bitboard AllPieces;
};

Concept of the Lookup Table

Now that we can define a chess board, we can start to define some useful operations with the chess board. Some of these operations involve lookup tables. A lookup table is a very simple entity. In its simplest form, it is an array of precomputed values of an index number. Remember when I said that the board symbol, like A1 was equivalent to bit 0 of a bitboard and vice versa? This is where that equivalence begins to pay off. The index into a lookup table will be either a bit position, or a chess board position (A1, B2, etc). The returned value from the lookup table will be a bitboard representing the all possible moves (as if the piece was alone on the board at that position) or a bitboard representing possible attacks (also as if the piece was alone).

The first lookup tables we will be making will be ClearRank, MaskRank, ClearFile, and MaskFile. These represent operations which return certain slices of the chess board. Each lookup table will have 8 entries. ClearRank and MaskRank will return bitboard that when ANDed with another bitboard, either a rank will be removed (all bits set to zero) or kept (all other places but that row set to zero). ClearFile and MaskFile do the same thing, but for a file instead of a rank--so of course there will be eight each of these as well. Here is what they look like. The green squares represent the bits that will be passed by the logical AND, the blue squares represent those that will be set to zero.

This is the complete description of the lookup table of ClearRank.

This is the complete description of the lookup table of MaskRank.

This is the complete description of the lookup table of ClearFile.

This is the complete description of the lookup table of MaskFile.

Here is an example of how to use these. For example, suppose I want to see which white pawns are in rank 3. It might be done like this:

WhitePawnsRank3 = WhitePawns & MaskRank[RANK_3];

Let's see what this looks like visually:

The Piece Lookup Table

This lookup table is very simply a lookup table which translates a position on a chess board to a bitboard containing a 1 on the position marker and a zero everywhere else. This is useful to represent an arbitrary piece alone on in a bitboard for further computation purposes, especially when dealing with human input, e.g., a human types that they wish to move a white pawn from d3 to d4. So, Piece[D3] would represent a bitboard that you could use in the computation of where that single piece could validly move. Here is a small example of such a lookup table:

Piece[A1] =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
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
Piece[A1]:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

Piece[B1] =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 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
Piece[B1]:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

Piece[C1] =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 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
Piece[C1]:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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

And so on, following the previously described winding...