Understanding “o^(o-2r)” formula for generating sliding piece moves using unsigned bitboards?

狂风中的少年 提交于 2021-01-28 20:14:32

问题


What I Am Trying To Do I am trying to perform some bitwise operations to create a chess engine. To make this engine, I need to be able to generate moves for pieces, like rooks. There is a handy formula for creating a bitboard of squares available for the rook to move to: bitboardOfOccupiedSquares ^ (bitboardOfOccupiedSquares - 2 * bitboardOfPieceToMove).

Consider the following chess board position:

I am trying to generate all of the squares that the rook on h1 can move to. So this should be easy, I simply grab the bitboard of occupied squares (18410433801713942527) and grab the bitboard for the rook on h1 (2^63 or 9223372036854775808) and plug them into the equation:

let bitboardOfOccupiedSquares: UInt64 = 18410433801713942527
let bitboardOfPieceToMove: UInt64 = 9223372036854775808
let bitboardOfSquaresPieceCanMoveTo: UInt64 = bitboardOfOccupiedSquares ^ (bitboardOfOccupiedSquares - 2 * bitboardOfPieceToMove)

My Issue The problem I am facing is that there is no value for bitboardOfOccupiedSquares that is larger than 2 * (2^63), so the operation (bitboardOfOccupiedSquares - 2 * bitboardOfPieceToMove) always produces a negative number when passing 2^63 as the value for bitboardOfPieceToMove. Of course, negative numbers cannot be represented with unsigned integers, so the program crashes on me whenever a piece on h1 is passed.

I have seen a youtuber accomplish this method by using signed integers in Java (as seen here and here). I have tried using signed bitboards instead of unsigned bitboards throughout my engine, but this simply causes other issues to crop up in other places. Plus I know that most engines make use of unsigned bitboards with no issues.

Furthermore, 2 * (2^63) equals 18446744073709551616, which is one above the UInt64 max of 18446744073709551615, which I figure has to do with the whole "2s complement" idea.

What I'm Wondering Has anybody in the chess engine programming world worked with this o^(o-2r) formula, especially with unsigned bitboards? I am able to grasp the ideas conveyed in the article and youtube videos, but can't seem to make it work in practice with unsigned bitboards.

来源:https://stackoverflow.com/questions/60632425/understanding-oo-2r-formula-for-generating-sliding-piece-moves-using-unsign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!