3 ms·
To add to some of the uses of popcount, it is also used in chess engines. Since a chess board has 64 squares, many engines use 64 bit unsigned integers to repre
by agubelu 3y ago
To add to some of the uses of popcount, it is also used in chess engines. Since a chess board has 64 squares, many engines use 64 bit unsigned integers to represent the whole board, mapping each square to a bit, which are called bitboards. Then, each combination of piece type and color is given its own bitboard, for example, a bitboard to represent the positions of white rooks. The squares attacked by a piece can similarly be represented by a bitboard.
This in turn allows for the use of something called magic bitboards, which are essentially compressed lookup tables for the squares to which a rook or a bishop can move for every possible disposition of the board. Having all these possibilities precomputed speeds up move generation significantly.
Popcount is used, for example, to know the amount of pieces of a certain type on the board, the number of squares to which a piece can move, or the amount of pieces attacking a given square.