10

In C++, what is the rationale for == and != having higher precedence than bitwise AND, XOR, and OR?

It would seem to me more natural to have operator== and operator!= come after operator&, operator^, and operator|. I'd like to understand the motivation so that I can better remember the ordering.

For example, I would think the following kind of usage would be common:

if (bitFields & value == 0) { // Incorrect test.
  // Do Something.
}

Since the == result is either 1 or 0, why would you ever want to use it for bitwise operations? Instead, the above must be written as:

if ((bitFields & value) == 0) { // Correct test.
  // Do Something.
}

to get the intended meaning where the bitwise AND is done before the comparison to zero.

Matt
  • 21,026
  • 18
  • 63
  • 115
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • 6
    The rationale for C++ is, that C uses the same precedence rules. You should change the question to C. – nosid Jun 07 '12 at 13:17
  • 2
    possible duplicate of [C Operator precedence (bitwise & lower than ==)](http://stackoverflow.com/questions/4685072/c-operator-precedence-bitwise-lower-than) – assylias Jun 07 '12 at 13:20

1 Answers1

6
  1. It is historical from C
  2. Consider using functions in your if statement

e.g.

if (func1() == 2 & func2() == 3)

With the precedence of == being higher that & ensures that both functions are called.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • 2
    That does not seem to be proper use of a bitwise AND. – Matt Jun 07 '12 at 13:20
  • 1
    @Matt, It's a perfectly justified use: no short-circuiting. – chris Jun 07 '12 at 13:21
  • What if `func1()` returns 1 and `func2()` returns 2? – Matt K Jun 07 '12 at 13:23
  • @mkb Then 1 == 2 => 0. 2 == 3 => 0. 0 & 0 => 0. – Matt Jun 07 '12 at 13:24
  • My mind blocked out the `==` operators some how. Need more coffee. – Matt K Jun 07 '12 at 13:25
  • 4
    @chris Even so, that doesn't seem to be enough justification for the precedence rules. `if ((func1() == 2) & (func2() == 3))` is not that difficult to write on the rare occasion that you need to. Comparing bitwise operations, however, is not so rare. – Matt Jun 07 '12 at 13:28
  • @Matt, True, I agree with that. – chris Jun 07 '12 at 13:42
  • 2
    @Ed: Part of your answer is misleading. Precedence has nothing to do with whether both functions are called. Even if it were switched, both would still be called; the result of the expression is the only thing that would change. – R.. GitHub STOP HELPING ICE Jun 07 '12 at 14:07
  • @Matt: it's historical from before `&&` and `||` were introduced to the language. Originally, `&` and `|` were all that was available, so this behavior made more sense. – Stephen Canon Jun 07 '12 at 14:35
  • @StephenCanon `&&` and `||` existed when K&R was first published. Does anything before K&R really even count? – Matt Jun 07 '12 at 15:11
  • 1
    @Matt: Yes; see the "Neonatal C" section of http://cm.bell-labs.com/cm/cs/who/dmr/chist.html – Stephen Canon Jun 07 '12 at 15:18
  • @R - Consider if the precedence was switched, end up computing `2 & func2()` first to get a value (say `x`). Now the expression becomes `func1() == x == 3`! – Ed Heal Jun 07 '12 at 16:31
  • @StephenCanon Thanks, that was an interesting article! – Matt Jun 07 '12 at 18:24