Wil*_*mKF 9 c c++ bitwise-operators operator-precedence comparison-operators
在C++中,==和!=的优先级高于按位AND,XOR和OR的基本原理是什么?
这似乎对我来说更自然的事operator==和operator!=跟从operator&,operator^和operator|.我想了解动机,以便我能更好地记住订购.
例如,我认为以下类型的用法很常见:
if (bitFields & value == 0) { // Incorrect test.
// Do Something.
}
Run Code Online (Sandbox Code Playgroud)
由于==结果是1或0,为什么你要将它用于按位运算?相反,上述内容必须写成:
if ((bitFields & value) == 0) { // Correct test.
// Do Something.
}
Run Code Online (Sandbox Code Playgroud)
在比较为零之前获得按位AND的预期含义.
if语句中使用函数例如
if (func1() == 2 & func2() == 3)
Run Code Online (Sandbox Code Playgroud)
优先级==更高,&确保调用两个函数.