First a
is random because the shift count is greater than the length of the type. This is undefined behavior:
/tmp/test.c: In function 'main':
/tmp/test.c:5:6: warning: left shift count >= width of type [enabled by default]
unsigned int a = 1 << 32;
^
Now b
is declared as long int
, but its value is computed with 32 bit logic, since by default 1 is int
:
/tmp/test.c:6:8: warning: left shift count >= width of type [enabled by default]
unsigned long int b = 1 << 32;
^
The correct expression for b would be:
unsigned long int b = 1L << 32;
EDIT to answer your new question:
In C, 1 is by default signed int
therefore 1 << 31
is the negative number -2147483648. Now this number gets converted to 64 bits, it becomes 0xffffffff80000000 (-2147483648 as signed long int) and finally its signedness is discarded. That's why you get 18446744071562067968 which is 0xffffffff80000000.