How to use bitboards in gdscript

The value is not stored any differently, the only difference is in how the value is displayed. Using "%x" % value I formatted the value as a hexadecimal string for printing to the log.

writing a value in binary notation in your code also makes no difference, it’s still just an int value. The purpose is to be able to read the individual bits in the source code easily, but for example 0b110 and 6 produce the exact same value.

The reason I needed a function to create a bitfield for all 64 bits manually, is because the top bit in a signed int (one that can store negative values) is treated differently, and Gdscript doesn’t already provide a way to treat an int as a unsigned value, where the top bit isn’t special.

The function I wrote takes an int value of 1 or 0 for the highest bit, and a positive int value for the rest. (negative values have the top bit set) And it creates an int value that combines the top bit and the other 63 bits from the second argument. This is not useful for using the number as a signed value, it is specifically to create a number with certain bits set, since you can write all 64 bits out using binary notation. like (1 bit, the other 63).

The shift operation works on all the bits at once. if you want a single bit moved you must shift a value that only has that single bit set.

You could then combine it with other bits using the “bitwise or” operator |. Look up how bitwise operations work if you need more help understanding.

2 Likes