Magic BitBoard C Chess Programming Question

41 Views Asked by At

I'm working on writing a chess game in C (getting instruction via BlueFeverSoft.com but instructor/professor not responding to Twitter queries).

I need some help in trying to understand the following C Function as well as how/why the BitTable array was constructed:

const int BitTable[64] = {
     63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2,
     51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52,
     26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28,
     58, 20, 37, 17, 36, 8
};

int PopBit(U64 *bb) {
     U64 b = *bb ^ (*bb - 1);   // bb xor (bb - 1)
     unsigned int fold = (unsigned) ((b & 0xffffffff) ^ (b >> 32));
     *bb &= (*bb - 1);
     return BitTable[(fold * 0x783a9b23) >> 26];
}

I can treat it like a "black box" and passing in a U64 bitboard to the function properly "pops off" (removes) the lowest Square64 piece from the board.

However...I'd really prefer not to have a black box at this point. I understand the basic programming mechanics but not some of the 'magic' stuff:

  1. What is the relation of the BitTable array to the 64 position chessboard.
    • I understand there are 64 numbers in the array, representing the chessboard but I DON'T understand the order of the numbers within the array
  2. What is 0x783a9b23? and why is it being shifted right 26 bits?

If somebody has the time..an explanation of each line would be greatly appreciated!

THANKS TONS!

NOTE: The above array and function just magically appeared in the 9th video with no explanation of them at all. The only reference the instructor made was "you can find these on the Chess Wiki". I researched the Chess Wiki and found tons of references to Magic BitBoards but....nothing specifically addressing the above array and function.

Have been researching this like crazy on the web but haven't found any specific answers.

0

There are 0 best solutions below