1

The function below is attempting to create a bitboard with the set bit being in the Nth position, by bitshifting 0x1 N times to achieve desired result. N is being given by the 1-6th least significant bits in a uint16_t. It is then masked to isolate 6 lsbs.

uint64_t endSquareFinder(uint16_t a){
    a &= (0x003f);
    return 0x0000000000000001 << a;
}

All inputs work except for when a = 0x001f, the output of the function is 0xffffffff80000000 rather than 0x0000000080000000. This to me is very bizare.

gdb compiler

Aamir
  • 1,974
  • 1
  • 14
  • 18
  • 1<<0x3f is 0x800000000000, which is either a large positive number or a very negative number, depending on how it's being interpreted. It looks to me that there are type conversions when calling printBitboard or printHex4, but without seeing those declarations, there's no way to tell. – user1806566 Jul 06 '23 at 17:50
  • 1
    Looks a lot like a 32->64 bit sign extension. – user4581301 Jul 06 '23 at 17:51
  • 1
    @TedLyngmo Sorry that was debugging code, I had just made it look better as not to get roasted :/ whoops – Michael Haney Jul 06 '23 at 17:51
  • @user4581301 that's the solution!! Thank you :) I needed to declare it as a uint64_t before modifying it, there was clearly an error in the compiling (ted :) ) so it was being treated as 32bit i guess? – Michael Haney Jul 06 '23 at 17:54
  • 1
    Even though you have a buttload of 0s in `0x0000000000000001`, it's still just plain ol' 1 to the compiler and easily fits in the default signed `int` – user4581301 Jul 06 '23 at 18:05

1 Answers1

6

You need to make 1 into an unsigned 64 bit integer. Right now it's an int ...

#include <type_traits>
// this passes:
static_assert(std::is_same_v<decltype(0x0000000000000001), int>);

... which is most likely only 32 bits.

Example:

return std::uint_least64_t(1) << a;
// or
return 1ull << a; // "unsigned long long int" is at least 64 bits
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108