Bitboard
   HOME

TheInfoList



OR:

A bitboard is a specialized
bit array A bit array (also known as bitmask, bit map, bit set, bit string, or bit vector) is an array data structure that compactly stores bits. It can be used to implement a simple set data structure. A bit array is effective at exploiting bit-level p ...
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
commonly used in computer systems that play
board game Board games are tabletop games that typically use . These pieces are moved or placed on a pre-marked board (playing surface) and often include elements of table, card, role-playing, and miniatures games as well. Many board games feature a comp ...
s, where each bit corresponds to a game board space or piece. This allows parallel bitwise operations to set or query the game state, or determine moves or plays in the game. Bits in the same bitboard relate to each other by the rules of the game, often forming a game position when taken together. Other bitboards are commonly used as masks to transform or answer queries about positions. Bitboards are applicable to any game whose progress is represented by the state of, or presence of pieces on, discrete spaces of a gameboard, by mapping of the space states to bits in the data structure. Bitboards are a more efficient alternative board representation to the traditional ''mailbox'' representation, where each piece or space on the board is an array element. Bitboards are especially effective when the associated bits of various related states on the board fit into a single word or double word of the CPU architecture, so that single bitwise operators like AND and OR can be used to build or query game states. Among the computer game implementations that use bitboards are
chess Chess is a board game for two players, called White and Black, each controlling an army of chess pieces in their color, with the objective to checkmate the opponent's king. It is sometimes called international chess or Western chess to disti ...
,
checkers Checkers (American English), also known as draughts (; British English), is a group of strategy board games for two players which involve diagonal moves of uniform game pieces and mandatory captures by jumping over opponent pieces. Checkers ...
,
othello ''Othello'' (full title: ''The Tragedy of Othello, the Moor of Venice'') is a tragedy written by William Shakespeare, probably in 1603, set in the contemporary Ottoman–Venetian War (1570–1573) fought for the control of the Island of Cypru ...
and
word games Word games (also called word game puzzles or word search games) are spoken, board, or video games often designed to test ability with language or to explore its properties. Word games are generally used as a source of entertainment, but can add ...
. The scheme 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.


Description

A bitboard, a specialized bit field, is a format that packs multiple related boolean variables into the same machine word, typically representing a position on a board game, or state of a game. Each bit represents a space; when the bit is positive, a property of that space is true. Bitboards allow the computer to answer some questions about game state with one bitwise operation. 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 then 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, such as 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, so that answering a question like "what are the legal moves of a knight on space e4?" can be answered by a single memory fetch. Bitfield 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 to be efficient. Bitboards may not be effective on earlier 8- and 16-bit minicomputer and microprocessor architectures.


Implementation issues

As a result of necessary compression and encoding of the contents of massive tables and the probability of transcription or encoding errors, bitboard programs are tedious for software developers to either write or debug. Auxiliary generative methods not part of the application are usually required to build the tables.


Processor use


Pros

Bitboard representations use parallel bitwise operations available on nearly all CPUs that complete in one cycle and are fully pipelined and cached etc. Nearly all CPUs have
AND or AND may refer to: Logic, grammar, and computing * Conjunction (grammar), connecting two words, phrases, or clauses * Logical conjunction in mathematical logic, notated as "∧", "⋅", "&", or simple juxtaposition * Bitwise AND, a boole ...
, OR, NOR, and
XOR Exclusive or or exclusive disjunction is a logical operation that is true if and only if its arguments differ (one is true, the other is false). It is symbolized by the prefix operator J and by the infix operators XOR ( or ), EOR, EXOR, , ...
. Furthermore, modern CPUs have
instruction pipeline In computer engineering, instruction pipelining or ILP is a technique for implementing instruction-level parallelism within a single processor. Pipelining attempts to keep every part of the processor busy with some instruction by dividing inco ...
s 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 and therefore increase pipelining and make effective use of multiple execution units on many CPUs. CPUs have a bit width which they are designed toward and can carry out bitwise operations in one cycle in this width. So, on a 64-bit or more CPU, 64-bit operations can 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. So a program using 64-bit bitboards would run faster on a 64-bit processor than on a 32-bit processor.


Cons

Bitboard representations have much longer code, both source and object code. Long bit-twiddling sequences are technically tricky to write and debug. The bitboards themselves are sparse, sometimes containing only a single one bit in 64, so bitboard implementations are memory-intensive. Both these issues may increase cache misses or cause cache thrashing. If the processor does not have hardware instructions for 'first one' (or '
count leading zeros In computer software and hardware, find first set (ffs) or find first one is a bit operation that, given an unsigned machine word, designates the index or position of the least significant bit set to one in the word counting from the least signifi ...
') and ' count ones' (or 'count zeros'), the implementation will be significantly handicapped, as these operations are extremely inefficient to code as assembly language loops.


Cache and memory use


Pros

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''.


Cons

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. For mobile devices (such as cell phones) with a limited number of registers or processor instruction cache, this can cause a problem. 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.


Incremental update

Some kinds of bitboards are derived from others by an elaborate process of cross-correlation, such as the attack maps in chess. Reforming all these maps at each change of game state (such as a move) can be prohibitively expensive, so derived bitmaps are incrementally updated, a process which requires intricate and precise code. This is much faster to execute, because only bitmaps associated with changed spaces, not all bitmaps over the board, need to change. Without incremental update, bitmapped representation may not be more efficient than the older mailbox representation where update is intrinsically local and incremental.


Precomputed bitmaps and table lookup

Some kinds of bitmaps that don't depend on board configurations can be precomputed and retrieved by table lookup rather than collated after a move or state change of the board, such as spaces attacked by a knight or king located on each of 64 spaces of a chessboard that would otherwise require an enumeration.


Chess bitboards

The obvious, and simplest representation of the configuration of pieces on a chessboard, is as a list (array) of pieces in a conveniently searchable order (such as smallest to largest in value) that maps each piece to its location on the board. Analogously, collating the spaces attacked by each piece requires a serial enumeration of such spaces for a piece. This scheme is called ''mailbox addressing''. Separate lists are maintained for white and black pieces, and often for white and black pawns. The maps are updated each move, which requires a linear search (or two if a piece was captured) through the piece list. The advantage of mailbox is simple code; the disadvantage is linear lookups are slow. Faster, but more elaborate data structures that map pieces to locations are called ''bitboards''.


Standard

In bitboard representations, each bit of a 64 bit word (or double word on 32-bit architectures) is associated with a square of the chessboard. Any mapping of bits to squares can be used, but by broad convention, bits are associated with squares from left to right and bottom to top, so that bit 0 represents square a1, bit 7 is square h1, bit 56 is square a8 and bit 63 is square h8. Many different configurations of the board are usually represented by their own bitboards including the locations of the kings, all white pawns, all black pawns, as well as bitboards for each of the other piece types or combinations of pieces like all white pieces. Two attack bitboards are also universal: one bitboard per square for all pieces attacking the square, and the inverse bitboard for all squares attacked by a piece for each square containing a piece. Bitboards can also be constants like one representing the first rank, which would have one bits in positions 0 - 7. Other local or transitional bitboards like "all spaces adjacent to the king attacked by opposing pieces" may be collated as necessary or convenient. An example of the use of the bitboards would be determining whether a piece is ''
en prise This glossary of chess explains commonly used terms in chess, in alphabetical order. Some of these terms have their own pages, like ''fork'' and ''pin''. For a list of unorthodox chess pieces, see Fairy chess piece; for a list of terms specific t ...
'': bitboards for "all friendly pieces guarding ''space''" and "all opposing pieces attacking ''space''" would allow matching the pieces to readily determine whether a target piece on ''space'' is ''en prise''. One of the drawbacks of standard bitboards is collating the attack vectors of the sliding pieces (rook, bishop, queen), because they have an indefinite number of attack spaces depending on other occupied spaces. This requires several lengthy sequences of masks, shifts and complements per piece.


Auxiliary bitboard representations

In acknowledgement of the code size and computing complexity of generating bitboards for the attack vectors of sliding pieces, alternate bitboard data structures have been devised to collate them. The bitboard representations of knights, kings, pawns and other board configurations is unaffected by the use of auxiliary bitboards for the sliding pieces.


Rotated bitboards

Rotated bitboards are complementary bitboard data structures that enable tabularizing of sliding piece attack vectors, one for file attack vectors of rooks, and one each for the diagonal and anti-diagonal attack vectors of bishops (rank attacks of rooks can be indexed from standard bitboards). With these bitboards, a single table lookup replaces lengthy sequences of bitwise operations. These bitboards rotate the board occupancy configuration by 90 degrees, 45 degrees, and/or 315 degrees. A standard bitboard will have one byte per rank of the chess board. With this bitboard it's easy to determine rook attacks across a rank, using a table indexed by the occupied square and the occupied positions in the rank (because rook attacks stop at the first occupied square). By rotating the bitboard 90 degrees, rook attacks up and down a file can be examined the same way. Adding bitboards rotated 45 degrees and 315 degrees (-45 degrees) produces bitboards in which the diagonals are easy to examine. The queen can be examined by combining rook and bishop attacks. Actually rotating a bitboard is an inelegant transformation that can take dozens of instructions.


Direct hashing

The rank and file attack vectors of rooks and the diagonal and anti-diagonal attack vectors of bishops can be separately masked and used as indices into a hash table of precomputed attack vectors depending on occupancy, 8-bits each for rooks and 2-8 bits each for bishops. The full attack vector of a piece is obtained as the union of each of the two unidirectional vectors indexed from the hash table. The number of entries in the hash table is modest, on the order of 8*2^8 or 2K bytes, but two hash function computations and two lookups per piece are required., see the hashing scheme employed.


Magic bitboards

Magic bitboards are an extrapolation of the time-space tradeoff of direct hashing lookup of attack vectors. These use a transmutation of the full attack vector as an index into the hash table. ''Magic'' is a misnomer, and simply refers to the generation and use of a
perfect hash function In computer science, a perfect hash function for a set is a hash function that maps distinct elements in to a set of integers, with no collisions. In mathematical terms, it is an injective function. Perfect hash functions may be used to imp ...
in conjunction with tricks to reduce the potential size of the hash table that would have to be stored in memory, which is 8*2^64 or 144
exabyte The byte is a unit of digital information that most commonly consists of eight bits. Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit ...
s. The first observation is that the ''outer squares'' or first and eighth ranks together with the 'a' and 'h' files are irrelevant to the occupancy of the attack vector: the piece attacks those squares or not (depending on other blocking pieces) regardless of occupancy, so these can be eliminated from consideration, leaving just 6x6 or 36 squares (~bits in the corresponding hash function). As with other schemes which require a perfect hashing function, an exhaustive process of enumeration, partly algorithmic and partly trial and error, is necessary to generate the hash function. But the intractable issue remains: these are very active tables, and their size (less than a million entries in most cases) is huge relative to the lower level cache sizes of modern chip architectures, resulting in cache flooding. So magic bitboards in many applications provide no performance gain over more modest hashing schemes or rotated bitboards.


History

The bitboard method for representing a board game appears to have been invented in the mid-1950s, by Arthur Samuel and was used in his checkers program. For the more complicated game of chess, it appears the method was independently rediscovered later by the
Kaissa Kaissa (russian: Каисса) was a chess program developed in the Soviet Union in the 1960s. It was named so after Caissa, the goddess of chess. Kaissa became the first world computer chess champion in 1974 in Stockholm. History By 1967, a c ...
team in the
Soviet Union The Soviet Union,. officially the Union of Soviet Socialist Republics. (USSR),. was a transcontinental country that spanned much of Eurasia from 1922 to 1991. A flagship communist state, it was nominally a federal union of fifteen national ...
in the late 1960s, and again by the authors of the U.S. Northwestern University program "
Chess Chess is a board game for two players, called White and Black, each controlling an army of chess pieces in their color, with the objective to checkmate the opponent's king. It is sometimes called international chess or Western chess to disti ...
" in the early 1970s. The 64-bit word length of 1970s super computers like Amdahl and Cray machines, facilitated the development of bitboard representations which conveniently mapped the 64-squares of the chessboard to bits of a word. Rotated bitboards for collating the moves of sliding pieces were invented by Professor Robert Hyatt, author of Cray Blitz and Crafty chess engines, sometime in the mid-1990s and shared with the Dark Thought programming team. They were later implemented in Crafty and Dark Thought, but the first published description wasn't until 1997. A decade later, direct lookup methods using masked ranks, files and diagonals to index a table of attack vectors depending on occupancy states of bits under the mask, were introduced. One such scheme utilizing a perfect hash function to eliminate hash collisions, was termed "magic bitboards". Nonetheless, the large size and high access rates of such tables caused memory occupancy and cache contention issues, and weren't necessarily more efficacious than the rotated bitboard approach. Today, game programs remain divided, with the best scheme being application dependent.


Other games

Many other games besides chess benefit from bitboards. * In
Connect Four Connect Four (also known as Connect 4, Four Up, Plot Four, Find Four, Captain's Mistress, Four in a Row, Drop Four, and Gravitrips in the Soviet Union) is a two-player connection board game, in which the players choose a color and then take tur ...
, they allow for very efficient testing for four consecutive discs, by just two shift + AND operations per direction. * In the
Conway's Game of Life The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further ...
, they are a possible alternative to arrays. * Othello/Reversi (see the
Reversi Reversi is a strategy board game for two players, played on an 8×8 uncheckered board. It was invented in 1883. Othello, a variant with a fixed initial setup of the board, was patented in 1971. Basics There are sixty-four identical game pieces ...
article).


See also

*
Board representation (chess) Board representation in computer chess is a data structure in a chess program representing the position on the chessboard and associated game state. Board representation is fundamental to all aspects of a chess program including move generation, ...


Notes


References

{{reflist, refs= {{cite book , author-last1=Atkin , author-first1=Larry R. , author-last2=Slate , author-first2=David J. , editor-first=Peter W. , editor-last=Frey , date=1983 , orig-year=1977 , edition=2 , publisher=
Springer Verlag Springer Science+Business Media, commonly known as Springer, is a German multinational publishing company of books, e-books and peer-reviewed journals in science, humanities, technical and medical (STM) publishing. Originally founded in 1842 in ...
, isbn=0-387-90790-4 , chapter=Chess 4.5: the Northwestern University Chess Program , title=Chess Skill in Man and Machine , citeseerx=10.1.1.111.926 , pages=82–118
{{cite journal , title=How Dark Thought Plays Chess , author-last=Heinz , author-first=Ernst A. , date=September 1997 , journal=
ICCA Journal The ''ICGA Journal'' is a quarterly academic journal published by the International Computer Games Association. It was renamed in 2000. Its previous name was the ''ICCA Journal'' of the International Computer Chess Association, which was founded i ...
, volume=20 , issue=3 , pages=166–176 , url=http://supertech.lcs.mit.edu/~heinz/dt/node2.html
{{cite web , title=Rotated Bitboards: New Twist on an Old Idea , author-last=Hyatt , author-first=Robert , author-link=Robert Hyatt , date=1999 , url=http://www.cis.uab.edu/info/faculty/hyatt/bitmaps.html, archive-url=https://web.archive.org/web/20050428023318/http://www.cis.uab.edu/info/faculty/hyatt/bitmaps.html , archive-date=28 April 2005 {{cite journal , author-last=Tannous , author-first=Sam , edition=2 , date=2007-07-23 , orig-year=2006 , title=Avoiding Rotated Bitboards with Direct Lookup , journal=
ICGA Journal The ''ICGA Journal'' is a quarterly academic journal published by the International Computer Games Association. It was renamed in 2000. Its previous name was the ''ICCA Journal'' of the International Computer Chess Association, which was founded in ...
, volume=30 , issue=2 , location=Durham, North Carolina, USA , pages=85–91 , arxiv=0704.3773v2 , citeseerx=10.1.1.561.3461 , doi=10.3233/ICG-2007-30204
{{cite book , author-last=Knuth , author-first=Donald , author-link=Donald Knuth , date=1973 , title=The Art of Computer Programming , title-link=The Art of Computer Programming , volume=3 , chapter=Section 6.4. Algorithm D (Open addressing with double hashing) {{cite web , title=Magic Bitboards Explained! , author-first1=Michael , author-last1=Sherwin , author-first2=Gerd , author-last2=Isenberg , work=Winboard Forum , date=2006-12-04 , quote=Call it Kindergarten Bitboards , url=http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=5958 {{cite web , title=Fast(er) bitboard move generator , author-first=Lasse , author-last=Hansen , work=Winboard Forum , date=2006-06-14 , url=http://www.open-aurec.com/wbforum/viewtopic.php?f=4&t=5015. {{cite journal , date=1959 , title=Some Studies in Machine Learning Using the Game of Checkers , journal= IBM Journal of Research and Development {{cite journal , date=1970 , title=Programming a computer to play chess , journal=
Russian Mathematical Surveys ''Uspekhi Matematicheskikh Nauk'' (russian: Успехи математических наук) is a Russian mathematical journal, published by the Russian Academy of Sciences and Moscow Mathematical Society and translated into English as ''Russia ...


Further reading

* http://people.csail.mit.edu/heinz/dt/node2.html


External links


Calculators


64 bits representation and manipulation


Checkers



by Jonathan Kreuzer


Chess


Articles


Bitboards - Chessprogramming wiki



Laramee, Francois-Dominic. Chess Programming Part 2: Data Structures.

Verhelst, Paul. Chess Board Representations




* ttp://www.onjava.com/pub/a/onjava/2005/02/02/bitsets.html Pepicelli, Glen. ''Bitfields, Bitboards, and Beyond'' - (Example of bitboards in the Java Language and a discussion of why this optimization works with the Java Virtual Machine (www.OnJava.com publisher: O'Reilly 2005))
Magic Move-Bitboard Generation in Computer Chess. Pradyumna Kannan


Code examples



The author of the Frenzee engine had posted some source examples.

A 155 line java Connect-4 program demonstrating the use of bitboards.


Implementations


= Open source

=
Beowulf
Unix, Linux, Windows. Rotated bitboards. *
Crafty Crafty is a chess program written by UAB professor Dr. Robert Hyatt, with continual development and assistance from Michael Byrne, Tracy Riegle, and Peter Skinner. It is directly derived from Cray Blitz, winner of the 1983 and 1986 World Compu ...
See the Crafty article. Written in straight C. Rotated bitboards in the old versions, now uses magic bitboards. *
GNU Chess GNU Chess is a free software chess engine and command-line interface chessboard. The goal of GNU Chess is to serve as a basis for research, and as such it has been used in numerous contexts. GNU Chess is free software, licensed under the terms ...
See the GNU Chess Article. *
Stockfish Stockfish is unsalted fish, especially cod, dried by cold air and wind on wooden racks (which are called "hjell" in Norway) on the foreshore. The drying of food is the world's oldest known preservation method, and dried fish has a storage lif ...
UCI chess engine ranking second in Elo as of 2010
Gray Matter
C++, rotated bitboards. * KnightCap GPL. ELO of 2300.
Pepito
C. Bitboard, by Carlos del Cacho. Windows and Linux binaries as well as source available.
Simontacci
Rotated bitboards.


=Closed source

= * DarkThough
Home Page


Othello


A complete discussion
of Othello (
Reversi Reversi is a strategy board game for two players, played on an 8×8 uncheckered board. It was invented in 1883. Othello, a variant with a fixed initial setup of the board, was patented in 1971. Basics There are sixty-four identical game pieces ...
) engines with some source code including an Othello bitboard in C and assembly. * Edax (computing) See the Edax article. An Othello (
Reversi Reversi is a strategy board game for two players, played on an 8×8 uncheckered board. It was invented in 1883. Othello, a variant with a fixed initial setup of the board, was patented in 1971. Basics There are sixty-four identical game pieces ...
) engine with source code based on bitboard.


Word games


Overview of bit board usage in word games.
Game artificial intelligence Digital tabletop games Computer chess Bit data structures