Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Strange error when bitshifting uint_64 by a uint_16 in cpp

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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::uint64_t(1) << a;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading