Why the following code, compiled with gcc, prints "ffffffff 0" instead of "0 0"? The bits are shifted to the right by 32 positions in both instructions. It doesn't make much sense, since x == 32, but still this strange result happens...
int x = 32;
printf("%x\n", 0xffffffff >> x);
printf("%x\n", 0xffffffff >> 32);
Assuming int and unsigned int are 32 bit wide, the integer constant
0xffffffff is of type unsigned int.
Right shifting an integer with a value of greater or equal the width of the integer will result in undefined behavior1.
This happens in both cases in your example.
Undefined behavior means that anything can happen. Getting the value 0xffffffff instead of 0 fits this behavior.
You cannot shift by the width of the integer type, because it says so in the standard. You must make a check if the value is greater or equal the width of the type your working with.
1 (Quoted from: ISO/IEC 9899:201x 6.5.7 Bitwise shift operators 3)
If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.