The Beauty of Bitboards

I really am enjoying coding Maverick using bitboards.  Yesterday I stumbled across a neat way to handle en-passant move generation.

moves = (((board->piecelist[piece] << 7) >> 16 * to_move) & B8H1) & (board->occupied[opponent] | board->ep_square);

The term “board->ep_square” is a bitboard holding the en-passant target capture square.  The last “or” instruction fools the move generation routine into thinking there is piece at this target square.  This means en-passant moves are then handled the same as all other pawn captures (über-cool!!).

  • what exactly is B8H1?

    • Steve Maughan

      B8H1 is a bitboard constant with all bits set corresponding to the rectangle whose corners are B8 and H1 i.e. a bit board defined as ~(A File). It’s used to mask the bits which can capture in a North West direction from white’s point of view.

  • Andrew Brooks

    What is to_move? is that the board index ( 0 to 63 ) ? I am programming a chess engine in java for fun, and I want to steal your line of code. (:-)

    Of course, you can reciprocate. My code is maybe half original, half borrowed code snippets from dozens of different sources.

    • Steve Maughan

      Hi Andrew – “to_move” is the side to move and “opponent” is the side which isn’t moving. It’s bitboard architecture so most of the terms are 64 bit unsigned integers, although the “t_board” record does have squares[0..63] which hold the piece on each square.

  • Andrew Brooks

    I wrote my own code for rotated bitboards for rook moves, based on the ideas in
    because I was lucky enough to find a simple function to rotate a bitboard by 90 degrees.
    However, I never could find a pair of functions to do 45 degree rotations for bishops. This seems kind of tough to do from scratch just using shifts and other bit operations.