C types: uint64_t less than 64 bits?

This C code is underlined in the IDE and gives me an error:

uint64_t dummy = 1<<50;
Also:
unsigned long long dummy = 1<<50;

The “<<” is underlined, and hovering shows “Shift count >= width of type”.

I noticed this because this was not working either:
printf("%" PRIu64 "\n, dummy);
and I thought to just manually print each bit for debugging.
I’ve just realized I need to #include <inttypes.h> - this resolves the printf problem, but the 1<<50 problem is still present.

Am I doing something wrong (again)?

1 Like

I think numbers default to int, you would need force the type: uint64_t dummy = (uint64_t)1<<50;

2 Likes

No. That does not work because you are trying to shift a 32 bit int which gives you a … 32 bit int. Casting the result to 64 bit does not change its value. What you must do is 1ull<<50 because 1ull is a 64 bit int with value 1

2 Likes

When manipulating uint64_t I usually define 1 and 0 as uint64_t variables to avoid manipulating 32bit int by mistake:

const uint64_t ONE = 1;
const uint64_t ZERO = 0;
const uint64_t ONES = ~ZERO;

uint64_t dummy = ONE << 50;
3 Likes

Thanks boys!
Sorry for the noise!