A chess engine asks which squares a piece attacks millions of times during one search. A bitboard stores a set of squares in a single integer, so a question about the whole board becomes a few arithmetic operations instead of a loop over 64 squares. This lesson pairs with Coding Adventure: Making a Better Chess Bot by Sebastian Lague, linked under Watch & read. You build the bit tools, shifts that respect the board edges, knight, king and pawn attacks, sliding attacks that stop at blockers, and passed-pawn detection, and you grade them against a slow version that walks the board square by square. The video also has chapters on the lookup-table method for sliding pieces; here you use shifts and the lowest or highest set bit instead.
Number the squares along the ranks: a1 = 0, b1 = 1, up to h1 = 7, then a2 = 8, and so on up to h8 = 63. Bit s of an integer is 1 when square s belongs to the set. Union is | and intersection is &. Python integers have no fixed width, so a left shift can push bits past square 63. Mask with (1 << 64) - 1 after every left shift, or the board quietly grows.
Two expressions take squares out of a set. b & -b keeps only the lowest set bit, and b & (b - 1) removes it.
b = 0b101100 # squares 2, 3 and 5
low = b & -b
print(bin(low), low.bit_length() - 1, bin(b & (b - 1)))Shifting moves a whole set at once. North is << 8 and south is >> 8. East looks like << 1, but a piece on the h file would land on the a file of the next rank. The fix is a mask applied after the shift. An east-moving shift (<< 1, << 9, >> 7) clears file a, which is 0xfefefefefefefefe. A west-moving shift (>> 1, << 7, >> 9) clears file h, which is 0x7f7f7f7f7f7f7f7f. The mask names the file the wrapped bits land on, not the file they started from, and that is the easiest thing to reverse. Knight jumps that move two files need masks for two files at once.
MASK = (1 << 64) - 1
NOT_A = 0xFEFEFEFEFEFEFEFE
h1 = 1 << 7
print((h1 << 1) & MASK, (h1 << 1) & NOT_A & MASK) # 256 is square a2Knights, kings and pawns follow from shifts. A king's attacks are its square shifted in all eight directions and combined. A knight uses eight shifts by 17, 15, 10, 6 and their negatives, each with its own mask. White pawns attack by shifting north-east and north-west, and the same call works for every pawn at once. A knight on a1 attacks only c2 and b3, a quick test for the masks.
A sliding piece needs its attacks to stop at the first piece in the way. For each square and direction, build the empty-board ray once by shifting one bit repeatedly until it falls off the board. Then, for a given position, AND the ray with the occupied squares to get the blockers. If there are none, the whole ray stands. Otherwise find the nearest blocker, and remove every square behind it by XOR with the empty-board ray that starts at that blocker. The blocker stays in the result: the piece attacks it, as a capture or as a defended friend.
In the example above the rook's north ray is d5, d6, d7, d8. The blocker d7 = 51 is the only one, so d8 is cut off. The result has ten squares and equals the integer 0x8080876080800.
The choice of nearest blocker is where this goes wrong. Going north the squares grow, so the nearest blocker is the lowest set bit. Going south they shrink, so the nearest is the highest. A rook on d4 looking south sees d3 = 19, d2 = 11 and d1 = 3. With pieces on d2 and d1, the nearest is 11, the larger number.
south = [19, 11, 3] # d3, d2, d1 below a rook on d4
blockers = (1 << 11) | (1 << 3) # pieces on d2 and d1
low = (blockers & -blockers).bit_length() - 1
high = blockers.bit_length() - 1
print(low, high)A rook is the union of four rays, a bishop of the other four, and a queen is both.
A pawn is passed when no enemy pawn stands ahead of it on its own file or a neighbouring one. To test every pawn at once, shift the enemy pawns one step towards their own side, fill that direction to the edge of the board, and widen the result by one file to each side. That span holds every square an enemy pawn can still guard or block. Your pawns outside the span are passed. An enemy pawn beside yours, on the same rank, is behind it and does not count.
Three mistakes cause most bugs. Masks the wrong way round give shifts that look fine mid-board and leak across the edge, so a piece on the h file attacks the a file. The lowest bit used on a south or west ray cuts it at the far blocker and includes squares the piece cannot reach. And Python integers are unbounded, so a bit above 63 survives until something compares it with the true answer.
The tests compare every function with a version that moves one square at a time using file and rank coordinates, on all 64 squares, all eight directions and many random positions, including empty and full boards. Agreement is strong evidence that the bit tricks are right. It does not show that they are fast, and it covers attack sets only: no pins, no check, no legality, and a pawn push is not an attack. Move generation builds on these sets.