这段代码让我很困惑:
bool b = 8 & 7 == 0; //b == false
std::cout << 8 & 7; //Outputs 0
为什么要这样做?
最佳答案
http://en.cppreference.com/w/c/language/operator_precedence
==
在 &
之前执行/求值,所以你得到:
bool b = 8 & 7 == 0; //==>
// 7==0 --> 0
// 8 & 0 --> 0 (which is 'false')
// ==> b is false
要获得您期望的结果,只需添加 ()
:
bool b = (8 & 7) == 0; // will be evaluated as 'true'
关于c++ - 这个按位运算是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37255022/