I am developing a simple C app on a CentOS linux machine my university owns and I am getting very strange inconsistent behavior with the <<
operator.
Basically I am attempting to shift 0xffffffff
left based on a variable shiftNum
which is based on variable n
int shiftNum = (32 + (~n + 1));
int shiftedBits = (0xffffffff << shiftNum);
This has the effect of shifting 0xffffffff
left 32-n
times and works as expected. However when n = 0
and shiftNum = 32
I get some very strange behaviour. Instead of getting the expected 0x00000000
I get 0xffffffff
.
For example this script:
int n = 0;
int shiftNum = (32 + (~n + 1));
int shiftedBits = (0xffffffff << shiftNum );
printf("n: %d\n",n);
printf("shiftNum: 0x%08x\n",shiftNum);
printf("shiftedBits: 0x%08x\n",shiftedBits);
int thirtyTwo = 32;
printf("ThirtyTwo: 0x%08x\n",thirtyTwo);
printf("Test: 0x%08x\n", (0xffffffff << thirtyTwo));
Outputs:
n: 0
shiftNum: 0x00000020
shiftedBits: 0xffffffff
ThirtyTwo: 0x00000020
Test: 0x00000000
I have no idea what is going on honestly. Some crazy low-level something I suspect. Even more strange the operation (0xffffffff << (shiftNum -1)) << 1
outputs 0x00000000
.
Does anyone have any clue whats going on?