我在 msdev 2010 中看到 __int64 类型的一些奇怪行为。有人能告诉我发生了什么吗?我想这里有两个问题,首先如何显示 64 位整数,其次是行为 - 即为什么看起来 __int64 实际上是一个 32 位整数...
#include <stdio.h>
int main()
{
int vl_idx;
unsigned __int64 vl_64;
unsigned __int64 vl_64_test;
for (vl_idx = 0; vl_idx < 64; vl_idx++)
{
vl_64 = 1 << vl_idx;
printf ("vl_64 (%d) = %I64u\n", vl_idx, vl_64);
printf ("vl_64 (%d) = %llu\n", vl_idx, vl_64);
printf ("vl_64 (%d) = %lu\n", vl_idx, vl_64);
}
vl_64_test = 1 << 31;
if (vl_64 > vl_64_test)
printf ("greater\n");
if (vl_64 == vl_64_test)
printf ("equal\n");
if (vl_64 < vl_64_test)
printf ("less\n");
return 0;
}
前 30 次迭代的输出与预期的一样:
vl_64 (0) = 1
vl_64 (0) = 1
vl_64 (0) = 1
vl_64 (1) = 2
vl_64 (1) = 2
vl_64 (1) = 2
...
vl_64 (30) = 1073741824
vl_64 (30) = 1073741824
vl_64 (30) = 1073741824
vl_64 (31) = 18446744071562067968
vl_64 (31) = 18446744071562067968
vl_64 (31) = 2147483648
vl_64 (32) = 1
vl_64 (32) = 1
vl_64 (32) = 1
vl_64 (33) = 2
vl_64 (33) = 2
vl_64 (33) = 2
...
vl_64 (62) = 1073741824
vl_64 (62) = 1073741824
vl_64 (62) = 1073741824
vl_64 (63) = 18446744071562067968
vl_64 (63) = 18446744071562067968
vl_64 (63) = 2147483648
equal
但随后的事情?溢出?在第 32 次迭代。这可能只是一个显示问题,但最后的比较表明并非如此。这是使用 msdev 2010 cl(64 位版本)编译并在 64 位 Windows 操作系统(具有 64 位 CPU)上运行的。关于为什么比较声明 1<<31 == 1<<63 的任何建议?
感谢您的任何建议,
吉姆