gpt4 book ai didi

c++ - 为什么以下两个程序会产生不同的输出? (C++,位运算,VS 2012)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:41:56 25 4
gpt4 key购买 nike

程序 1:

int x = 4 ^ (4>>32);
cout << x << endl;

输出为4

程序 2:

int x = 4;
int y = x ^ (x>>32);
cout << y << endl;

输出为0

最佳答案

如果 int 具有 32 位或更少,则两个代码片段都会引发未定义的行为。 [expr.shift]/1:

The behavior is undefined if the right operand is negative, or greater than or equal to the length in bits of the promoted left operand.

因此,实现没有义务以任何方式提供一致的结果。

关于c++ - 为什么以下两个程序会产生不同的输出? (C++,位运算,VS 2012),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27084470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com