I am currently trying to implement magic bitboards into my chess engine. Let's just talk about rooks here. Given a square, what I can currently do is:

-Calculate the sliding moves on an empty board.

  • Generate all blocker boards.
  • Calculate the corresponding mve board.

What I am having trouble with is the using of this information. I have downloaded an array of length 64, populated by "magic numbers" for the rook squares. Can I use this? I can generate a index from this information, but it doesn't correspond to the correct move board. Any ideas? I am happy to share more information and code upon a request.

1 Answer 1


It sounds like you have all the tools to do the job, you just need to put it all together. And yes, you can use the magic numbers you found.

When your engine initializes (or any time before you begin the search, really) you'll need to pre-compute the move board for every possible permutation of blockers you might encounter during the search.


for each square...
  ROOK_MASKS[square] = //rook attacks on an empty board
  int bits = bitCount(ROOK_MASKS[square]);
  ROOK_SHIFTS[square] = 64 - bits;

  int permutations = 1 << bits
  for each permutation (p)...
    int index = (ROOK_MAGICs[square] * ROOK_MASKS[square]) >>> ROOK_SHIFTS[square];
    long blockers = //the pth permutation of ROOK_MASKS[square]
    ROOK_ATTACKS[square][index] = //rook attacks with blockers

Then when it comes time to generate moves you need to perform a similar operation using the pieces actually on the board

Move Generation

  long blockers = ROOK_MASKS[square] & //all the pieces on the board
  int index = (ROOK_MAGICS[square] * blockers) >>> ROOK_SHIFTS[square];
  return ROOK_ATTACKS[square][index];
  • Thank you for your reply. Since posting this, I have managed to get it work in a slightly different way. When my engine is finished, I will revisit this and see if I can improve it! Commented Jun 28, 2020 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.