实验环境:
Win7 32位 vc++6.0
实验代码:
int main(int argc, char* argv[])
{
unsigned char a = 0xA5;
//1.
// unsigned char b = (a=~a)>>4+1;
//2.
unsigned char b = (~a)>>4+1;
printf("b=%d\n", b);
return 0;
}
实验结果:
第一种情况时,b=2
第二种情况时,b=250
实验分析:
1.汇编代码的比较
10: unsigned char b = (a=~a)>>4+1;
0040102C mov eax,dword ptr [ebp-4]
0040102F and eax,0FFh
00401034 not eax
00401036 mov byte ptr [ebp-4],al
00401039 mov ecx,dword ptr [ebp-4]
0040103C and ecx,0FFh
00401042 sar ecx,5
00401045 mov byte ptr [ebp-8],cl
11: unsigned char b = (~a)>>4+1;
0040102C mov eax,dword ptr [ebp-4]
0040102F and eax,0FFh
00401034 not eax
00401036 sar eax,5
00401039 mov byte ptr [ebp-8],al
3.此实验要注意~a在寄存器中的运算,以及寄存器的位数32。