3 ms·
Is there an explanation on why C’s operator precedence is weird? Such as: why does the bitwise AND have higher precedence than logical AND?
by colejohnson66 6y ago
Is there an explanation on why C’s operator precedence is weird? Such as: why does the bitwise AND have higher precedence than logical AND?
- briantakita 6y ago> why does the bitwise AND have higher precedence than logical AND? Why is this precedence weird? Bitwise AND tends to be used to transform data while a logical AND tends to be used for control flow.
- colejohnson66 6y agoI meant equals having a higher precedence than bitwise AND. As in: if (x & 2 == 2) ...is actually parsed as: if (x & (2 == 2)) ...which isn’t intuitive.
- compiler-guy 6y agoSee the above example from dmr himself
- compiler-guy 6y agoThere is, and it is amusing “In retrospect it would have been better to go ahead and change the precedence of & to higher than ==, but it seemed safer just to split & and && without moving & past an existing operator. (After all, we had several hundred kilobytes of source code, and maybe 3 installations....)“ https://www.lysator.liu.se/c/dmr-on-or.html https://www.lysator.liu.se/c/dmr-on-or.html