问题描述
#include <iostream>
using namespace std;
int main()
{
cout << "sizeof(unsigned int): " << sizeof(unsigned int) << endl;
unsigned a = 1;
int i = 0;
while (a) {
cout << "a[" << i++ << "]: " << a << endl;
a <<= 1;
}
cout << "a[" << i << "]: " << a << endl;
unsigned b = 1;
unsigned c = (b << 31);
unsigned d = (b << 32);
cout << "c: " << c << endl;
cout << "d: " << d << endl;
return 0;
}
/ *输出 * /
/* Output http://www.compileonline.com/compile_cpp_online.php */
Compiling the source code....
$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In function 'int main()':
main.cpp:19:23: warning: left shift count >= width of type [enabled by default]
Executing the program....
$demo
sizeof(unsigned int): 4
a[0]: 1
a[1]: 2
a[2]: 4
a[3]: 8
a[4]: 16
a[5]: 32
a[6]: 64
a[7]: 128
a[8]: 256
a[9]: 512
a[10]: 1024
a[11]: 2048
a[12]: 4096
a[13]: 8192
a[14]: 16384
a[15]: 32768
a[16]: 65536
a[17]: 131072
a[18]: 262144
a[19]: 524288
a[20]: 1048576
a[21]: 2097152
a[22]: 4194304
a[23]: 8388608
a[24]: 16777216
a[25]: 33554432
a[26]: 67108864
a[27]: 134217728
a[28]: 268435456
a[29]: 536870912
a[30]: 1073741824
a[31]: 2147483648
a[32]: 0
c: 2147483648
d: 1
问题>正如你可以看到 a [32
等于0.现在为什么 d
is not 0
but 1
?
Question> As you can see a[32
is equal to 0. Now why d
is NOT 0
but 1
?
推荐答案
这是C / C ++中未定义的行为。
This is undefined behavior in C/C++.
标准特别留下这个未定义,因为不同的CPU将做不同的事情,转变。具体来说,在32位Intel上,我相信只有移位量的低5位被CPU使用,其余位被忽略。如果我记得正确,PowerPC和64位Intel都使用低6位,并忽略其余。
The standard specifically leaves this undefined because different CPUs will do different things when presented with such a shift. Specifically, on 32-bit Intel, I believe only the low 5 bits of the shift amount are used by the CPU, and the rest of the bits are just ignored. If I remember correctly, PowerPC and 64-bit Intel both use the low 6 bits and ignore the rest.
更高级的语言可能会尝试通过更正结果在逻辑上更一致,但是像C / C ++这样的低级语言被设计为接近金属,并且将希望为<
运算符。
A higher level language might attempt to smooth this out by correcting the results to be more logically consistent, but a low-level language like C/C++ is designed to be "close to the metal" and will want to generate a single bit-shift instruction for the <<
operator.
这篇关于为什么左移无符号数32次不会产生ZERO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!