Har*_*sen 1 c++ visual-c++ visual-c++-2013
这是一个当前在Windows 10上运行的32位MFC应用程序.使用Visual C++ 2013编译.
std::cout << "sizeof(long long) = " << sizeof(long long) << std::endl;
int rot{ 32 };
long long bits{ (1 << rot) };
std::cout << "bits with variable = " << bits << std::endl;
long long bits2 = (1 << 32);
std::cout << "bits2 with constant = " << bits2 << std::endl;
system("pause");
Run Code Online (Sandbox Code Playgroud)
长的大小是8个字节,足以管理我的32位,我在想.以下是调试版本的输出:
sizeof(long long) = 8
bits with variable = 1
bits2 with constant = 0
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
这是发布版本的输出:
sizeof(long long) = 8
bits with variable = 0
bits2 with constant = 0
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
因此,显然即使使用64位数据类型,我的单位也会被遗忘.但是我真的很困惑为什么调试版本会产生不同的输出,如果我将变量作为参数与常量相比移位?
您需要一个long long64位的类型.
1 << 32将使用int操作数的类型来计算表达式,而不管分配此结果的变量的类型如何.
你会有更多的运气1LL << 32,和1LL << rot.这导致使用long long类型计算表达式.
目前,程序的行为是未定义的,因为您在编写时会过度移动类型1 << 32.另请注意,这1 << 32是一个编译时可评估的常量表达式,而1 << rot不是.这可能是使用变量和常数之间观察到的差异的原因.
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |