Bitboards implement a finite set of up to 64 elements - all the squares of a chessboard. There is a bijective one-to-one correspondence between bits of a bitboard and the squares of a board, but there are many different ways to create this mapping. Some various considerations on this mapping are listed here.

In the strict sense there are even three finite sets with bijective one-to-one correspondence.

A set of all squares on a chessboard for from a1 to h8 (A1 to H8). The 8 ranks of a board are labeled from 1 to 8.The 8 files of a board are labeled from 'a' to 'h' (or 'A' to 'H').

A set of all 64 squares enumerated from 0..63 (or 1..64).

The 64 bits inside a bitboard may be enumerated in different orders. (FirstOne versus LastOne, Forward versus Reverse).

Usually we define the bit-indices in arithmetical order to map bits inside a bitboard to numbers.

Bit index zero is the least significant bit (LSB = 2^0).

Bit index 63 is the most significant bit (MSB = 2^63).

The reversed ordering was used as well, motivated by the leading zero count instruction of certain processors.

In the following we rely on arithmetical bit-order order and focus on how to map squares to numbers, which applies to other 8*8 board representations as well.

Deduction on Files and Ranks

We can deduct square mapping on enumerating files and ranks from 0 to 7 each.
There are two common approaches to calculate the square-index from file or rank,
Least Significant File Mapping or Least Significant Rank Mapping.

LSR mapping has some advantages in calculating pawn attacks, since there are no wraps to consider. Anyway, more common is LSF-mapping where ranks are aligned to the eight consecutive bytes of a bitboard and we further rely on LSF-mapping.

Main article: Endianness
The question remains how to enumerate files and ranks. There are two different orders each, little-endian versus big-endian order of bits and bytes. Thus in total four possible alternatives. The drawback of little endian file mapping becomes aware if we write bitboards as binary or hexadecimal strings, since we usually write numbers big endian wise.

Anyway, further we rely on little-endian mapping of files and ranks though, since we retain a kind of "natural" relation - that is a < h and 0 < 7.

Rank 1 .. Rank 8 -> 0..7
A-File .. H-File -> 0..7

To convert from little-endian file mapping to big-endian file mapping and vice versa:

## Table of Contents

Home * Board Representation * Bitboards * Square Mapping Considerations## Square and Bitindex

Bitboards implement a finite set of up to 64 elements - all the squares of a chessboard. There is a bijective one-to-one correspondence between bits of a bitboard and the squares of a board, but there are many different ways to create this mapping. Some various considerations on this mapping are listed here.In the strict sense there are even three finite sets with bijective one-to-one correspondence.

Usually we define the bit-indices in arithmetical order to map bits inside a bitboard to numbers.

- Bit index zero is the least significant bit (LSB = 2^0).
- Bit index 63 is the most significant bit (MSB = 2^63).

The reversed ordering was used as well, motivated by the leading zero count instruction of certain processors.In the following we rely on arithmetical bit-order order and focus on how to map squares to numbers, which applies to other 8*8 board representations as well.## Deduction on Files and Ranks

We can deduct square mapping on enumerating files and ranks from 0 to 7 each.There are two common approaches to calculate the square-index from file or rank,

Least Significant File Mapping or Least Significant Rank Mapping.

LSR mapping has some advantages in calculating pawn attacks, since there are no wraps to consider. Anyway, more common is LSF-mapping where ranks are aligned to the eight consecutive bytes of a bitboard and we

further rely on LSF-mapping.## Endianness

Main article: EndiannessThe question remains how to enumerate files and ranks. There are two different orders each, little-endian versus big-endian order of bits and bytes. Thus in total four possible alternatives. The drawback of

little endianfile mapping becomes aware if we write bitboards as binary or hexadecimal strings, since we usually write numbersbig endianwise.Anyway, further we rely on little-endian mapping of files and ranks though, since we retain a kind of "natural" relation - that is a < h and 0 < 7.

To convert from little-endian file mapping to big-endian file mapping and vice versa:

To convert from little-endian rank mapping to big endian-rank mapping and vice versa:

One may combine both conversions in one step by xoring with 63.

## Little-Endian Rank-File Mapping

Most often used in CPW samples## Square Enumeration

Little endian rank-file (LERF) mapping implies following C++ enumeration:## Compass Rose

We rely on the compass rose to identify ray-directions with following increments to neighbored squares.## Some hexadecimal Constants

Some bitboard constants with LERF-mapping:## Little-Endian File-Rank Mapping

## Square Enumeration

Little endian file-rank (LEFR) mapping implies following C++ enumeration:## Compass Rose

We rely on the compass rose to identify ray-directions with following increments to neighbored squares.## Some hexadecimal Constants

Some bitboard constants with LEFR-mapping:## See also

## Forum Posts

## What links here?

Up one Level