Search code examples
javabitboardulong

How to properly use bitboards


I'm wondering what is the correct way to implement bitboards into a chess engine. So far I did some research on the basics of chess engine programming and took some notes, mostly stuff such as minimax, alphabeta, quiescence search and advanced evaluation functions.

Then I split my project into 3 parts which are:

  • GUI
  • Board representation
  • Implementing a search that goes beyond minimax and coming to a decent evaluation function for static positions

I'm using bitboards to represent the board state, to save time both when representing the board and when calculating the gametree. This is where my problem lies.

Since I didn't just want to copy someone else's code, I tried to create an unsigned long to test it out, but apparantly a single bit was lost.

Here's the code:

public static void main(String[] args) {
    Long bitBoard = Long.parseUnsignedLong("9223372036854775807");

    System.out.println("Number of bits in this 64-bit unsigned long: " + Long.bitCount(bitBoard));  
    System.out.println(Long.MAX_VALUE);
}

Can someone give me a hint as to what I did wrong, and why I'm losing a bit?


Solution

  • In java a Long will always be signed, the first bit(that you lost) determines if it is positive or negative. Long.parseUnsignedLong(...) will only make sure that the value you pass is a positive number, not actually convert the datatype into an unsigned long.

    EDIT: Nevermind. The above statement is incorrect. The issue you have is that you use the max value for a signed Long. The max value for an unsigned long is 18446744073709551615.