menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

左移32位?

昨天碰到一个诡异的问题,这段代码:

#include <stdio.h>
int main() {
unsigned int x = 0xffffffff;
int n = 32;
printf("%X\n", x << n);
return 0;
}

的输出结果,居然是“FFFFFFFF”!!!

经过测试,在vc和gcc下表现相同。

一开始以为是编译器的问题,于是用这段代码尝试了一下:

#include <stdio.h>
int main() {
unsigned int n = 0;
#ifdef _WIN32
__asm {
mov eax,0FFFFFFFFh
mov ecx,20h
shl eax,cl
mov dword ptr [n],eax
}
#else
__asm__ (
"movl $0xffffffff,%%eax;"
"movl $0x20,%%ecx;"
"shl %%cl,%%eax;"
"movl %0,%%eax":"=r"(n)
);
#endif
printf("%X\n", n);
}

结果也是相同的,还是“FFFFFFFF”。

看来是cpu的指令的问题了。
上网找了一下,80386 的指令系统, 有这么一段话:

The 8086 does not mask the shift count. However, all other Intel Architecture processors (starting with the Intel 286 processor) do mask the shift count to 5 bits, resulting in a maximum count of 31. This masking is done in all operating modes (including the virtual-8086 mode) to reduce the maximum execution time of the instructions.

好吧,那我们不左移32位了好吧。。。

刚才又做了一个测试:

#include <stdio.h>
int main() {
unsigned int n = 0xffffffff;
printf("%X\n", n << 32);
return 0;
}

在vc下会报告:warning C4293: '<<' : shift count negative or too big, undefined behavior
在gcc下会报告:warning: left shift count >= width of type

看来这个是个已知问题了,只是我第一次碰到罢了。

操,我完全看不懂了,废了啊,废了

64位环境如何?

流水:上面的代码在64位环境下测试过的,一样的情况。