ArticleslgStudy

computer science

Bitboard

Bitboard is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bitboard rather than just read about it. In short: A bitboard is a specialized bit array data structure commonly used in computer systems that play board games, where each bit corresponds to a game board space or piece. The technique is applicable to any board game whose game state is represented by the presence of pieces on discrete spaces of a gameboard, including chess, checkers, othello and word games.

Bitboard — main illustration
Bitboard — illustration

Key takeaways

  • Bitboard belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bitboard to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bitboard from memory before moving on to harder problems.

Reference excerpt

A bitboard is a specialized bit array data structure commonly used in computer systems that play board games, where each bit corresponds to a game board space or piece. The technique is applicable to any board game whose game state is represented by the presence of pieces on discrete spaces of a gameboard, including chess, checkers, othello and word games. It was first employed in checkers programs in the 1950s, and since the mid-1970s has been the de facto standard for game board representation in computer automatons. Compared with the traditional mailbox representation, where each piece or space on the board is an array element, bitboards are a more space-efficient board representation. Bitboards are often also more time-efficient. When the associated bits of related states on the bitboard fit into a single word or double word of the CPU architecture, single bitwise operators like AND and OR can be used to operate on the bitboard. These parallel bitwise operations can be much faster at setting and querying game states, and determining moves and plays in the game, than performing iterative array operations on a mailbox representation would be. Modern chess engines predominantly utilize magic bitboards, which employ perfect hashing to map a piece's occupancy mask directly to a pre-computed array of attack patterns in a single lookup operation.

Description

A bitboard is a specialized bit field: a format that represents the state of a board game by packing multiple Boolean variables into the same machine word. Each bit represents a space; when the bit is positive, a property of that space is true. Bitboards allow the computer to answer questions about game state using very few bitwise operations. For example, if a chess program wants to know if the white player has any pawns in the center of the board (center four squares), it can just compare a bitboard for the player's pawns with one for the center of the board using a bitwise AND operation. If there are no center pawns, the result will be all zero bits (i.e. equal to zero). Multiple bitboards may represent different properties of spaces over the board, and special or temporary bitboards (like temporary variables) may represent local properties or hold intermediate collated results. The efficacy of bitboards is augmented by two other properties of the implementation. First, bitboards are fast to incrementally update; for example, flipping the bits at the source and destination positions in a bitboard for piece location when a piece is moved. Second, bitmaps representing static properties like all spaces attacked by each piece type for every position on a chessboard can be pre-collated and stored in a table. This allows a question like "what are the legal moves of a knight on space e4?" to be answered by a single memory fetch. Bitboard implementations take advantage of the presence of fullword (32-bit or 64-bit) bitwise logical operations like AND, OR, NOT and others on modern CPU architectures in order for operations to be fast. Bitboards may not be as effective on earlier 8- and 16-bit minicomputer and microprocessor architectures.

Implementation Because the implementation of bitboards requires correct compression and encoding of massive tables and complex game states, bitboard programs can be tedious for software developers to write and debug.

Processor use

Advantages Bitboard representations use parallel bitwise operations available on nearly all CPUs that complete in one cycle, and are typically among the fastest instructions (being fully pipelined and cached, as well as other CPU-specific optimizations). Nearly all CPUs have the AND, OR, NOR, and XOR operations. Furthermore, modern CPUs have instruction pipelines that queue instructions for execution. A processor with multiple execution units can perform more than one instruction per cycle if more than one instruction is available in the pipeline. Normal instruction sequences with branches may cause the pipeline to empty if a branch is mispredicted. Many bitboard operations require fewer conditionals, increasing pipelining and making effective use of multiple execution units on many CPUs. CPUs have a bit width, the number of completable bitwise operations in one cycle. On a 64-bit or larger CPU, 64-bit operations can therefore occur in one instruction. There may be support for higher or lower width instructions. Many 32-bit CPUs may have some 64-bit instructions and those may take more than one cycle or otherwise be handicapped compared to their 32-bit instructions. If the bitboard is larger than the width of the instruction set, multiple instructions will be required to perform a full-width operation on it. A program using 64-bit bitboards would therefore run faster on a 64-bit processor than on a 32-bit processor.

Disadvantages Bitboard representations have much longer code, both source and object code. Long bit-twiddling sequences are technically tricky to write and debug. This issue may increase cache misses or cause cache thrashing. If the processor does not have hardware instructions for 'first one' (or 'count leading zeros') and 'count ones' (or 'count zeros'), the implementation will be significantly handicapped, as these operations are extremely inefficient to code as loops or other high-level constructs.

Cache and memory use

Advantages Bitboards require more memory than piece-list board data structures, but are more execution efficient because many loop-and-compare operations are reduced to a single (or small number of) bitwise operation(s). For example, in mailbox, determining whether piece attacks space requires generating and looping through legal moves of piece and comparing the final space with space. With bitboards, the legal moves of piece are stored in a bitmap, and that map is ANDed with the bitmap for space. A non-zero result means that piece attacks space.

Disadvantages For some games, writing a bitboard engine requires a fair amount of source code, including data tables that will be longer than the compact mailbox/enumeration implementation. This can cause problems for mobile devices (such as cell phones) with a limited number of registers or processor instruction cache. For full-sized computers, it may cause cache misses between level-one and level-two cache. This is only a potential problem, not a major drawback, as most machines will have enough instruction cache for this not to be an issue.

… excerpt ends here. Continue reading the full article.

Illustrations

Bitboard illustration

Worked examples

Example 1 — a first encounter with Bitboard

Start with the simplest possible case. Write down what Bitboard claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bitboard before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bitboard ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bitboard

In research
Bitboard appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bitboard in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bitboard is common in secondary-school and first-year university syllabi. It links to neighbouring topics Bit data structures, Computer chess, Digital tabletop games, so understanding it makes those chapters shorter.
In everyday life
Look for Bitboard outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bitboard” →

How to study Bitboard in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bitboard means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bitboard out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bitboard in simple terms?

A bitboard is a specialized bit array data structure commonly used in computer systems that play board games, where each bit corresponds to a game board space or piece. The technique is applicable to any board game whose game state is represented by the presence of pieces on discrete spaces of a ga…

Why does Bitboard matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bitboard?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bitboard.

Tags

  • Bit data structures
  • Computer chess
  • Digital tabletop games
  • Game artificial intelligence

Keep exploring