Counter-intuitive operator precedence
While debugging, I spotted a very counter-intuitive operator precedence evaluation.
Question: is it just me, or does the following code seem highly susceptible to bugs. If the code is indeed correct, does it serve as a nice example why we should be cautious about omitting parentheses? A warning would have saved me several hours of debugging...
3 Replies
It's the same as in Python (ref1, ref2).
Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation.
&
is "bitwise and", while I think the one you should have used is the logical and, namely and
, so A != B and test()
Yes, makes perfect sense, and I should have trusted the compiler to prevent an unnecessary jump with the logical operator.
Without seeing the generated IR or binary, I won’t be so sure about the branchless-ness of the result, but for very simple cases like this I think the compiler will just fold the
_ and test()
away.