unsigned int nn = ~0;
unsigned int ii = 0;
while( nn>>ii )
{
printf("%8x \n", nn>>ii);
ii++;
if(ii == 100)break;
}
本来我想让nn移位32位然后跳出循环的(*安全的做法当然是nn =>> 1;*),后来发现这个关系 :
(nn>>32 == nn)*粗浅的*查 , 书里面没有把>>的操作数超过32当作"未定义行为" .
从vc6里面看汇编,发现对应的指令是:
0040B89F mov edx,dword ptr [ebp-4]
0040B8A2 mov ecx,dword ptr [ebp-8]
0040B8A5 shr edx,cl
看起来C编译器没有对ii进行取模操作,而是直接把ii交给shr这个cpu指令
这是intel的文档原文:
[pdf 下载:http://www.intel.com/cd/ids/developer/apac/zho/dc/pentium4/reference/79962.htm ]
The SAL and SHL instructions perform the same operation (see Figure 7-6). They shift the source operand left by from 1 to 31 bit positions. Empty bit positions are cleared. The CF flag is loaded with the last bit shifted out of the operand.
我的理解: 虽然没说明shr 操作数是0和32的情况会发上什么,但是已经说明了1 to 31 , 所以默认把大于31的数对32取模,也算是理所当然的了。
另, 如果>>操作符后面是一个常数,例如 int n = nn >> 32 ;这样的语句, GCC编译器会报告一个警告: right shift count >= width of type
还是GCC周全一些啊.
--EOF--