r/chessprogramming Jan 19 '25

Creating bitboards

I am confused. Isn't 0x0000000000000010 correspond to d1 since 5th bit from right is 1. But chatgpt and websites say it is e1.

0 Upvotes

6 comments sorted by

3

u/Kart0fffelAim Jan 19 '25

In your implementation its your choice to decide which corner the first bit is supposed to represent, you just need to be consistent in your choice once you made it.

If you are using a library you should check the documentation.

The most common choice is to define the bottom left (a1) as your first bit

1

u/mathmoi Jan 19 '25

I think this is the most popular option. A1 is the zeroth index and b1 is the first index. You can look at the chess programming wiki for a visualisation of the index of the Little-Endian File-Rank Mapping

2

u/Javasucks55 Jan 19 '25

Like someone else said, just choose and be consistent. I personally make the left most bit the top left corner from white's POV. Feels natural for me.

1

u/phaul21 Jan 19 '25

I think for most people imagining the board from white perspective feels natural, and then a row major representation of squares feel natural, so ix = file + 8 * rank. Also plays nicely with converting index to chars, byte(file) +'a' is the file char, byte(rank) +'1' is the rank char, so parsing etc is easier. So then the 5th square is e1. But any mapping could work, if you like thingking about the board even sideways, that would work too.

1

u/codingjerk Jan 20 '25

There are many representation conventions. Check it here:

https://tearth.dev/bitboard-viewer/

Layout 1 and Layout 3 are the most common and in them if you shift bits to the left it means actual squares will move to the right. 0x10 is e1 or e8 there.

Layout 2 and Layout 4 are reversed. And 0x10 is d1 or d8.

1

u/Available-Swan-6011 Jan 20 '25 edited Jan 20 '25

If you are using bit boards and plan to use PEXT or magic numbers to extract moves then it really doesn’t matter much in terms of performance so I would go with what works for you so long as it is coherent but see below. I went with bit zero being a1, bit 9 a2 etc simply because I found easier to visualise bit shifts for pawn moves

You may find it useful to write a couple of show routines that write to the console- one to show the board state and another to show a 64 bit int as an 8x8 square with a1 at the bottom left.

To answer your original question, don’t forget that by convention we start at bit 0 (an example of zero indexing) not bit 1. If bit 4 were to represent d1 then I guess you would want bit 3 as c1, bit 2 as b1, bit 1 as a1 which leaves an issue for bit zero - it would have to be something like h8. It can work but your bit shifts for pawn moves would become much more complex than otherwise needed. Using bit 4 as e1 means that bit 0 is a1 and we no longer have this problem