Search code examples
cbitwise-operatorsoperator-precedence

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


In the C programing language, why do the bitwise operators (& and |) have lower precedence than the equality operator (==)? It does not make sense to me.


Solution

  • You need to ask Brian Kernighan or Dennis Ritchie.
    From the forum post Operator precedence:

    The && and || operators were added later for their "short-circuiting" behavior. Dennis Ritchie admits in retrospect that the precedence of the bitwise operators should have been changed when the logical operators were added. But with several hundred kilobytes of C source code in existence at that point and an installed base of three computers, Dennis thought it would be too big of a change in the C language...

    So, that might be a reason? I'm guessing since there are several layers of bitwise precedence (unlike relational comparisons) that it's cruft that's existed since...forever...and just was never corrected.