C++ is calculating wrong
C++ is calculating wrong
我有一个莫名其妙的问题。这是我的代码:
int result;
result = 0 * 2 ^ 1;
std::cout << result << std::endl;
此计算的结果为 1。但是为什么?实际上它应该是 0,不是吗? 我已经尝试过使用数学库的 pow 函数,但结果也是 1:
int result;
result = std::pow(0 * 2, 1);
std::cout << result << std::endl;
在C++和许多其他计算机编程语言中,^
意味着异或而不是幂。因此,它可能没有您期望的数学优先顺序。
对指数使用std::pow
:
const int result = std::pow(0 * 2, 1);
std::cout << result << 'n';
相关文章:
- C++ is calculating wrong
- Is std::is_trivially_copyable wrong?
- 使用 Boost ASIO 和 SSL 时出现"Wrong Version Number"错误 (C++)
- 由于错误'wrong version number'无法完成握手
- 使用强制转换为"wrong"类型的指针算术
- QMap<int,QByteArray> return Wrong QByteArray
- "Static polymorphism with Qt signal/slot: What is wrong?"
- What wrong with `(A.array() == A.array()).matrix()`?
- 如何识别"wrong"指针
- msys2 和标头位于'wrong'位置
- 如何解决'path is wrong'编译错误,包括 GTKmm 3.0
- Calculating PSD
- C++选择'wrong'默认参数的重载方法
- Wrong QModelIndex on QTreeView using custom QSortFilterProxy
- strcmp(x, str) is wrong?
- 代码块编译"wrong" cpp.sh 正确执行
- 代码厨师 Q - 为什么我会收到"Wrong Answer"错误
- Calculating Modbus RTU CRC 16
- c++ UVA 579 - ClockHands wrong answer
- VS-Android 在链接到共享库时生成"wrong"名称