Could someone explain the rationale, why in a bunch of most popular languages (see note below) comparison operators (==, !=, <, >, <=, >=) have higher priority than bitwise operators (&, |, ^, ~)?
I don't think I've ever encountered a use where this precedence would be natural. It's always stuff like:
if( (x & MASK) == CORRECT ) ... // Chosen bits are in correct setting, rest unimportant if( (x ^ x_prev) == SET ) // only, and exactly SET bit changed if( (x & REQUIRED) < REQUIRED ) // Not all conditions satisfied
The cases where I'd use:
flags = ( x == 6 | 2 ); // set bit 0 when x is 6, bit 1 always.
are near to nonexistent.
What was the motivation of language designers to decide upon such precedence of operators?
[arithmetics] [logic operator] [arithmetics]. Most programmers don't create a mess of parentheses like
if(((x+getLowX()) < getMinX) || ((x-getHighX())>getMaxX())))- most will assume precedence of arithmetics over logics and write
if( ( x + getLowX() < getMinX ) || ( x - getHighX() > getMaxX() ))assuming precedence of
<. Now intuitively
if( x ^ getMask() != PATTERN )should behave the same, XOR being arithmetic operator. The fact it's interpreted as
if( x ^ ( getMask() != PATTERN ) )is completely counter-intuitive.