Shift right ">>" in C99 [duplicate]
Possible Duplicate:
Weird behavior of right shift operator
Hello
Why both numbers from this function are printed the same? It is not a cyclic shift.开发者_如何学编程
unsigned int i=0x89878685;
int main()
{
printf("0x%x\n", i);
printf("0x%x\n", i>>32);
}
$ ./a.out
0x89878685
0x89878685
Do all compilers work in this way?
Shifting a 32-bit integer by 32 bits is undefined behavior. The result is not predictable.
In C and C++ if the integer has N
bits, you are only allowed to shift by less then N
bits. If you shift N
or more the behavior is undefined.
In practice, when shifting a 32-bit integer, some platforms will simply interpret the shift count as a 5-bit value (discard any bits above the lower 5), meaning that 32
will be interpreted the same way as 0
. This is apparently what happens on your platform. The value is not shifted at all.
精彩评论