开发者

Shift right ">>" in C99 [duplicate]

This question already has answers here: Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜