C語言中,移位操作是經常用到的
到時有個現象是i<<-1
和i<<31
的結果一樣
1 “-1”表示成補碼是1111 ….11 1111 ,31是 0000 …0011 1111,,他們的后六位是一樣的。
Interger的移位運算只注意后6位
Note also that rotation by any multiple of 32 is a no-op, so all but the last five bits of the rotation distance can be ignored, even if the distance is negative:
總結:將負數寫為補碼,這個數的低6位便是其真正移位的數字
int main()
{
int size = 1;
int sz1 = size << 1;
int sz2 = size << 193;
if (sz1 == sz2)
{
printf("sz1==sz2\n");
}
else
{
printf("sz1==sz2\n");
}
return 0;
}
結果 sz1==sz2