What is the reason behind giving &, ^ and | different precedence levels in C? I'm asking for C because most modern languages inherit similar precedence from C. But I don't know much about languages before C. Beside that, it's seems counter-intuitive for me to assign different precedences for operations that are almost identical.
I have heard that, in the beginning C didn't have logical operators. In that case they might had used the same operators for logical and bitwise, and hence they may have ended up with different precedence levels. But in that case, are there any languages, probably existing alongside or before C, which put all three operators at equal precedence?
In boolean logic, a disjunctive normal form (DNF) is a canonical normal form of a logical formula consisting of a disjunction of conjunctions; it can also be described as an OR of ANDs, a sum of products, or (in philosophical logic) a cluster concept.[citation needed] As a normal form, it is useful in automated theorem proving.
[...]
For example, all of the following formulas are in DNF:
- (A ∧ ¬B ∧ ¬C) ∨ (¬D ∧ E ∧ F)
The C equivalent of that example is:
(A && !B && !C) || (!D && E && F)
Since DNF is a very useful canonical form, it makes sense to make logical AND a higher precedence than logical OR so that the following is a DNF:
A && !B && !C || !D && E && F
Then it just make sense to follow the same pattern for bitwise and and bitwise or. As for the bitwise xor I can only speculate so I won't do that here.
Fun fact: both gcc and clang suggest parentheses around &&
and &
to avoid confusion for humans reading the code.