c++ - 等式运算符 == 右侧的按位运算符 ^ 未按预期工作
问题描述
代码片段:
void fn(){
if(14-2==0^2){
cout<<"14-2 is "<<14-2<<"\n";
cout<<"0^2 is "<<(0^2)<<"\n"; //cout<<0^2 shows error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'
cout<<"How is if evaluated to be true?";
}
else{
cout<<"else";
}
}
输出:
14-2 是 12
0^2 是 2
如果评估为真,如何?
我通过在 () 中包含 0^2 来解决问题,所以如果条件变为
if(14-2==(0^2))
这现在按预期工作。
问题:我想了解为什么如果评估为真
解决方案
14 - 2 == 0 ^ 2
被解析为
((14 - 2) == 0) ^ 2
如此(12 == 0) ^ 2
如此false ^ 2
如此2
(如此true
)
推荐阅读
- sql - 在 T-SQL 中使用 Window 函数计算类别列
- node.js - 未捕获的 ReferenceError: __webpack_dev_server_client__ 未定义
- c++ - 不同类的构造函数重载解析
- hyperlink - 如何在招摇文档中链接和引用自我?
- stripe-payments - 如何在条带元素中填充已保存的卡值?
- ruby - Cromedriver `driver.manage.logs.get(:browser)` 在 chromedriver 75.0.3770.8 上失败
- python-3.6 - 运行 python xlwings 库并失败 COMRetryObjectWrapper(DispatchEx('Excel.Application'))
- react-native - 在显示未处理的承诺拒绝时反应未定义的本机导航
- c# - 如何获取传递给我的函数的参数名称?
- powershell - Powershell 上的功能比较