pythonchessbitboard

Problem with moving bits in the bitboard to the left in Python


I am programming a chess ai. I ran into a problem when I want to move the bits from the king_span to the left. When I move the bits up to 45 places, it works fine. If I want to move them more than 45 places, the outputed bitboard is the same, as if it only moved by 45 places. Why is it not moving them any further and how could I possibly fix the problem? Do I have to make a second king_span for this?

Thanks for trying to help me.

king_span = int("0000000000000000000000000000000000000000000001110000010100000111", 2)

def print_bitboard(bitboard):
    board = '{:064b}'.format(bitboard)
    for i in range(8):
        print(board[8 * i + 0] + " " + board[8 * i + 1] + " " + board[8 * i + 2] + " " + board[8 * i + 3] + " " + board[
            8 * i + 4] + " " + board[8 * i + 5] + " " + board[8 * i + 6] + " " + board[8 * i + 7])




print_bitboard(king_span << 45)

Output:

1 1 1 0 0 0 0 0
1 0 1 0 0 0 0 0
1 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

--> Bits don't move further. The next bitboard should look like this:

1 1 0 0 0 0 0 0--> this bit gets deleted to zero
0 1 0 0 0 0 0 0--> this bit gets deleted to zero too
1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

Solution

  • Your initial number is 1110000010100000111 - that means you have 19 significant bits in the number.

    When you move the bits 45 binary steps to the left you end up with a number with 45 + 19 = 64 significant digits:

    1110000010100000111000000000000000000000000000000000000000000000

    If you move more than 45 steps, you will end up with more than 64 digits. Suppose you move 61 steps; your end result will have 80 digits:

     new_number = king_span << 61
     print('{:b}'.format(new_number))
    

    That will print

    11100000101000001110000000000000000000000000000000000000000000000000000000000000
    

    Since your for loop only prints 8 rows of 8 digits, that means you're only printing 64 digits. You have more; In this example you have 80 digits. The remaining digits are just not being printed.