Question

In some bitboard chess engines piece bitboards are initialized as follows:

white_pawns = 0x000000000000ff00
black_pawns = 0x00ff000000000000
white_knights = 0x000000000000042
black_knights = 0x4200000000000000
white_bishops =0x000000000000024
black_bishops = 0x2400000000000000
white_rooks = 0x000000000000081
black_rooks = 0x8100000000000000
white_queens = 0x0000000000000008
black_queens = 0x0800000000000000
white_king = 0x0000000000000010
black_king = 0x1000000000000000

Can someone please explain how these pieces get their respective hexadecimal values?

Was it helpful?

Solution

Each bit is being used to represent a position on a chess board, which is an 8x8 grid.

Since bits are being used, it's easier if you look at this in binary rather than hex. For instance, let's look at white_pawns in binary:

0000000000000000000000000000000000000000000000001111111100000000

Now let's divide that up into groups of eight:

00000000
00000000
00000000
00000000
00000000
00000000
11111111
00000000

Now you can clearly see that black is at the top, white is at the bottom, and the 1s indicate where white's pawns are at the beginning of the game.

As a second example, let's look at white_knights. It's missing one of its 0s (although it's harmless, it's bad form), so let's add that zero back: 0x00000000000042, which looks like this in binary:

0000000000000000000000000000000000000000000000000000000001000010

...which, in groups of eight, looks like this:

00000000
00000000
00000000
00000000
00000000
00000000
00000000
01000010

...which is indeed where white's knights would go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top