Operator priority (bitwise '&' lower than '==')

In the C programming language, why do bitwise operators (& and |) have lower priority than the equality operator (==)? It makes no sense to me.

+29
c bitwise-operators operator-precedence
Jan 13 2018-11-11T00:
source share
3 answers

You need to ask Brian Kernigan or Dennis Ritchie.
From this forum: http://bytes.com/topic/c/answers/167377-operator-precedence

& & and || operators were added later for their "short circuit" behavior. Dennis Ritchie assumes in retrospect that the priority of bitwise operators should have been changed when adding logical operators. But with a few hundred kilobytes of C source code that existed at this point, and an installed base of three computers, Dennis believed that it would be too much change in the C language ...

So, could that be the reason? I assume that there are several levels of bitwise evaluation (as opposed to relational comparisons) that he broke through, which has existed since then ... forever ... and simply has not been fixed.

+44
Jan 13 '11 at 20:55
source share

This also makes no sense to Dennis Ritchie, in retrospect.

http://www.lysator.liu.se/c/dmr-on-or.html

& & and || were added to the language after | and &, and priority supported for compatibility reasons.

+13
Jan 13 '11 at 20:59
source share

I have no authoritative answer as to why K & R chose the priority they made. One example that would make a lot of sense would be:

if (x == 1 & y == 0) { /* ... */ } 

Since this is a bitwise AND operator, it uses the evaluation mode without short circuit, as

 if (x == 1 | y == 0) { /* ... */ } 

use the short circuit operator OR. This is probably why they chose the priority group this way, but I agree with you that in retrospect this does not seem to be a good idea.

+3
Jan 13 '11 at 20:52
source share



All Articles