What are intuitive reasons for >=
and '>' to be higher precedence than |
? Reference.
I could understand why ||
is lower as writing a>b||c
is common. However I'd think a>b|c
would be doing an OR before the compare. This doesn't seem intuitive. What are examples of &^|
being lower precedence then compare/equity operators being intuitive?
-
4See this SO question/answer: [C operator precedence bitwiser lower than equality](http://stackoverflow.com/questions/4685072/c-operator-precedence-bitwise-lower-than). – Sean Perry Oct 11 '12 at 21:55
1 Answers
It's a historical accident, in The Development of the C Language, Dennis Ritchie wrote:
Their tardy introduction explains an infelicity of C's precedence rules. In B one writes
if (a==b & c) ...
to check whether a equals b and c is non-zero; in such a conditional expression it is better that & have lower precedence than ==. In converting from B to C, one wants to replace & by && in such a statement; to make the conversion less painful, we decided to keep the precedence of the & operator the same relative to ==, and merely split the precedence of && slightly from &. Today, it seems that it would have been preferable to move the relative precedences of & and ==, and thereby simplify a common C idiom: to test a masked value against another value, one must write
if ((a&mask) == b) ...
where the inner parentheses are required but easily forgotten.
So it's because B used |
and &
for the logical operators, and C kept the precedence for the thus-denoted bitwise operators.

- 181,706
- 17
- 308
- 431
-
1"Historical accident" seems a strange term to use for something that clearly wasn't an accident at all. Historical *artifact*, maybe. – Rob Kennedy Oct 11 '12 at 22:12
-
1@RobKennedy I had the impression that decisions that later turned out to be suboptimal were often tongue-in-cheek called "historical accident". I have seen the phrase used that way a couple of times before, or at least I interpreted it such. – Daniel Fischer Oct 11 '12 at 22:20
-