在C99标准中对于其右移操作的结果的规定是implementation-defined。在Linux上的GCC实现中,有符号数的右移操作的实现为使用符号位作为补充位。
示例代码:
<span style="font-size:18px;">#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int
main(void)
{
<span style="white-space:pre"> </span>int num = 0xf0000001;
<span style="white-space:pre"> </span>printf("num = %x\n", num>>1);
return 0;
}
</span>
<span style="font-size:18px;">
</span>运行结果:num = f8000000
所以,右移一位等于除以2是有条件的——操作数为正数。对于负整数除以2的操作目前想到的就只能想到的是 /2 了。
参考博客:http://blog.chinaunix.net/uid-23629988-id-3018793.html

349

被折叠的 条评论
为什么被折叠?



