You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: posts/visualizing-chess-bitboards.md
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,11 @@ tags: ["javascript"]
5
5
description: "Brief introduction to chess bitboards and move generation with animations."
6
6
---
7
7
8
-
Bitboards are an efficient way to store game state in (usually) 64-bit integers. There are 64 positions on a chess board so we can use each bit as an on/off switch.
8
+
When simulating board games on a computer, one of the challenges is keeping track of the game pieces. Bitboards are an efficient way to store game state in (usually) 64-bit integers. There are 64 positions on a chess board so we can use each bit as an on/off switch.
9
9
10
-
I want to describe a board where `f5` is occupied. We can use `67108864`.
10
+
Let's say I want to describe a board where `f5` is occupied. For that, we can use the number `67108864`. In decimal notation, it doesn't seem much like a chessboard. In hex, we can see that there's some structure: `0x0000000004000000`.
11
11
12
-
This number doesn't really scream that `f5` is occupied though.
13
-
14
-
I'll show it represented as a binary number with 64 digits.
12
+
For me, it starts to make more sense when representated as a binary number with 64 digits.
15
13
16
14
```text
17
15
0 0 0 0 0 0 0 0 I've added a line break
@@ -24,8 +22,6 @@ I'll show it represented as a binary number with 64 digits.
24
22
0 0 0 0 0 0 0 0
25
23
```
26
24
27
-
In my examples, I'll use hex representation. In this case that would be `0x0000000004000000`.
28
-
29
25
We can't pack an _entire_ game's state into a number (e.g. piece color, piece type, castling rights) so we use groups of numbers.
30
26
31
27
We can use _bitwise_ operations on these numbers to manipulate individual bits using operators like `AND`, `OR`, `XOR`, `NOT`, and bit shifts. These operations are fast (they're directly executed by the CPU in a single clock cycle with no need for complex computation or memory access).
@@ -60,7 +56,7 @@ Two numbers will probably stick out; `7` and `9`. If you picture that long row o
Without bitboards, a program has to perform many more (magnitudes more!) instructions. The equivalent code, without bitwise operations, would look something like this.
59
+
Without bitboards, a program has to perform many more (magnitudes more!) instructions. The equivalent code, without bitwise operations, would look something like this:
0 commit comments