Here's my code:
#include <stdio.h>
int main(int argc, char *argv[]) {
unsigned long int x = 0;
// trying to make x = 2,147,483,648
x = 1 << 31;
printf("%lu", x);
}
It's returning that x = 18446744071562067968. I read that unsigned long int should go up to 4,294,967,296, so why can't I use 1 << 32 to set x equal to 2,147,483,648?
1 << 31
causes undefined behaviour, if your system has 32-bit ints. The literal1
is a signed int.You need to do an unsigned shift instead of a signed shift:
I added
L
so that the code is still correct even on a 16-bit system, and it doesn't hurt to do so.Informally, shifting a
1
into the sign bit is undefined. The formal text can be found in section 6.5.7/4 of the C11 standard:Your other question, "why can't I use
1 << 32
" is covered by that same quote. What about1UL << 32
? If your system has 32-bitunsigned long
then this would also be undefined according to 6.5.7/3:But it would work if your system had 64-bit
unsigned long
. To avoid having your code break when compiled on a different system (this goal is known as code portability) you could write(uint64_t)1 << 32
(or1ULL << 32
) which is guaranteed to work.