Older Version Newer Version

GerdIsenberg GerdIsenberg Jun 23, 2014

[[toc]]
**[[Home]] * [[Board Representation]] * [[Bitboards]] * Bitboard Board-Definition**

To represent the board we typically need one bitboard for each [[Pieces#PieceTypeCoding|piece-type]] and [[Color|color]] - likely encapsulated inside a class or structure, or as an [[Array|array]] of bitboards as part of a [[Chess Position|position]] object. A one-bit inside a bitboard implies the existence of a piece of this piece-type on a certain [[Squares|square]] - one to one associated by the bit-position <ref>[[http://www.onjava.com/pub/a/onjava/2005/02/02/bitsets.html|Bitwise Optimization in Java: Bitfields, Bitboards, and Beyond]] by [[Glen Pepicelli]], 2005, [[http://en.wikipedia.org/wiki/O%27Reilly_Media|O'Reilly's]] [[http://onjava.com/|OnJava.com]]</ref>:
[[image:bitboard.gif link="http://www.onjava.com/pub/a/onjava/2005/02/02/bitsets.html?page=2"]] 

//To be aware of their scalar 64-bit origin, we use so far a type defined unsigned integer U64 in our [[C]]/[[Cpp|C++]] source snippets, the scalar 64-bit long in [[Java]]. Feel free to define a distinct type or wrap U64 into classes for better abstraction and type-safety during compile time.//
[[#CBoardDef]]
=Classical Board=
Those bitboards may part of a central position object which is [[Incremental Updates|incrementally updated]] while [[Make Move|making]] or [[Unmake Move|unmaking]] [[Moves|moves]]. 

==Structure==
[[code format="cpp"]]
class CBoard
{
   U64 whitePawns;
   U64 whiteKnights;
   U64 whiteBishops;
   U64 whiteRooks;
   U64 whiteQueens;
   U64 whiteKing;

   U64 blackPawns;
   U64 blackKnights;
   U64 blackBishops;
   U64 blackRooks;
   U64 blackQueens;
   U64 blackKing;
   ...
};
[[code]]

==Array==
For better generalization and to [[Avoiding Branches|avoid branches]], one may encapsulate [[Array|arrays]] of bitboards. For instance, inside the [[Beowulf]] sources (sample from moves.c) one finds a lot of branches on [[Side to move|side to move]] to either fetch white or black piece bitboards, as already criticized by [[Vincent Diepeveen]] in 2001 <ref>[[http://www.stmintz.com/ccc/index.php?id=173418|On Beowulf - long post]] by [[Vincent Diepeveen]], [[CCC]], April 04, 2001</ref> ...
[[code format="cpp"]]
  switch (side) {
   case WHITE: tsq = B->whiteRooks; break;
   case BLACK: tsq = B->blackRooks; break;
  }
[[code]]
.. where an indexed access with appropriate defined {0,1} color range for the side to move would avoid those branches, per piece-kind, ...
[[code format="cpp"]]
  tsq = B->rooks[side];
[[code]]
... or over all piece-kinds, ...
[[code format="cpp"]]
  tsq = B->pieceBB[nWhiteRook + side];
[[code]]
... for instance, on [[x86]] or [[x86-64]], utilizing its [[http://en.wikipedia.org/wiki/X86#Addressing_modes|addressing modes]] with base- and scalable [[http://en.wikipedia.org/wiki/Index_register|index register]], plus displacement:
[[code]]
   ; rsi pointer to structure, rcx side (0 == White, 1 == Black)
   mov  rax, qword ptr [rsi + rookOffset + 8*rcx] 
[[code]]
[[#Occupancy]]
Likely one also keeps some often used redundant [[General Setwise Operations#Union|union]] sets like white and black pieces, [[Occupancy|occupancy]] or their complement, the empty squares.
[[code format="cpp"]]
class CBoard
{
   U64 pieceBB[14];
   U64 emptyBB;
   U64 occupiedBB;
   ...
public:
   enum enumPiece
   {
      nWhite,     // any white piece
      nBlack,     // any black piece
      nWhitePawn, // white Pawn
      nBlackPawn, // white Pawn
      ...
   };

   U64 getPieceSet(PieceType pt) const {return pieceBB[pt];}
   U64 getWhitePawns() const {return pieceBB[nWhitePawn];}
   ...
   U64 getPawns(ColorType ct) const {return pieceBB[nWhitePawn + ct];}
   ...
};
[[code]]
On initialization and update of the bitboards, see [[General Setwise Operations#UpdateByMove|general setwise operations]].
[[#SixTwo]]
=Denser Board=
A common alternative to reduce the size of the board structure is to keep two color bitboards and six color independent piece bitboards, which are the [[General Setwise Operations#Union|union]] of black and white respective pieces, i.e. all queens. This space saving requires a cheap [[General Setwise Operations#Intersection|intersection]] of a color and a piece bitboard to get the required pieces of that color only. 
[[code format="cpp"]]
class CBoard
{
   U64 pieceBB[8];
public:
   enum enumPiece
   {
      nWhite,     // any white piece
      nBlack,     // any black piece
      nPawn,
      nKnight,
      nBishop,
      nRook,
      nQueen,
      nKing
   };

   U64 getPieceSet(PieceType pt) const {return pieceBB[pieceCode(pt)] & pieceBB[colorCode(pt)];}
   U64 getWhitePawns() const {return pieceBB[nPawn] & pieceBB[nWhite];}
   ...
   U64 getPawns(ColorType ct) const {return pieceBB[nPawn] & pieceBB[ct];}
   ...
};
[[code]]

=See also=
* [[Color Flipping]]
* [[Quad-Bitboards]]

=Forum Posts=
* [[http://groups.google.com/group/rec.games.chess.computer/browse_frm/thread/834fa3c273fafffe/cab7c12ea99e9a35|Bit Board Bonkers??]] by Dave, [[Computer Chess Forums|rec.games.chess.computer]], July 28, 1997
* [[http://www.stmintz.com/ccc/index.php?id=405590|Bitboard board representation]] by [[Eric Oldre]], [[CCC]], January 13, 2005
* [[http://www.talkchess.com/forum/viewtopic.php?t=17138|BitBoard representations of the board]] by [[Uri Blass]], [[CCC]], October 14, 2007
* [[http://www.talkchess.com/forum/viewtopic.php?t=47917|Decision concerning board representation]] by [[Piotr Lopusiewicz]], [[CCC]], May 05, 2013

=External Links=
* [[Videos#GlennGould|Glenn Gould]] - [[http://en.wikipedia.org/wiki/Paul_Hindemith|Paul Hindemith]], Piano Sonata No. 3 - Fugue, [[http://en.wikipedia.org/wiki/YouTube|YouTube]] Video
> [[media type="custom" key="24814126"]]

=References=
<references />
=What links here?= 
[[include page="Bitboard Board-Definition" component="backlinks" limit="40" ]]
**[[Bitboards|Up one Level]]**