I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
Convert them to arithmetic. If you ignore the casting, a && b is just a * b a || b is just a + b Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).
Dennis Ritchie on the priorities of && || vs. == etc. (1982)
11–20 of 178 posts
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#12Earlier quoted context omitted.
Convert them to arithmetic. If you ignore the casting, a && b is just a * b a || b is just a + b Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).
Is there an intuition for this correspondence otherwise I don't think it's very helpful
a + b + c is nonzero if any of them are nonzero. (Remember each value is either 0 or 1.) So that's the intuition for OR.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#13On the other hand I don't know if there's a real upside either. It's not very difficult to use && and || and it serves as extra documentation, and everyone is used to it by now.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#14Earlier quoted context omitted.
Convert them to arithmetic. If you ignore the casting, a && b is just a * b a || b is just a + b Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).
Is there an intuition for this correspondence otherwise I don't think it's very helpful
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#15There's a niche language called Sail that has & and | act as both bitwise and boolean operators. It actually works really well and isn't confusing at all after the initial "this is different". There's no real downside because it is a modern strictly typed language unlike C. On the other hand I don't know if there's a real upside either. It's not very difficult to use && and || and it serves as extra documentation, an…
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#16Earlier quoted context omitted.
Is there an intuition for this correspondence otherwise I don't think it's very helpful
a * b * c is only nonzero if all of a, b, c are nonzero. That's AND, and should be pretty intuitive. a + b + c is nonzero if any of them are nonzero. (Remember each value is either 0 or 1.) So that's the intuition for OR.
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#17I don't even know the priority between && and ||. If I'm using both, I use parentheses just so it's more explicit. "a && b || c" should flag a linter, IMO, with "(a && b) || c" or "a && (b || c)" being required.
Convert them to arithmetic. If you ignore the casting, a && b is just a * b a || b is just a + b Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).
My own mnemonic: SAXO. Shift, And, Xor, Or. (Like a real Saxo, it's fun to go a bit faster like this, but you do have to trust everybody involved, because any accident is probably going to end up badly for you.)
And now you know the bitwise precedence as well! And this ordering actually works out tidily for common operations: "x=a>n&m"; "x=x&~ma|a(Main annoying thing: a lot of compilers, even popular ones such as clang and gcc, don't actually seem to know what the precedence rules actually are, and generate warnings asking you to clarify. Presumably the authors didn't realise that C has an ISO standard, that can be consulted to answer this question? Very surprising.)
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#18Earlier quoted context omitted.
Convert them to arithmetic. If you ignore the casting, a && b is just a * b a || b is just a + b Now you remember the precedence between them (except in broken languages, of which the only notable one is shell).
Is there an intuition for this correspondence otherwise I don't think it's very helpful
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#19There's a niche language called Sail that has & and | act as both bitwise and boolean operators. It actually works really well and isn't confusing at all after the initial "this is different". There's no real downside because it is a modern strictly typed language unlike C. On the other hand I don't know if there's a real upside either. It's not very difficult to use && and || and it serves as extra documentation, an…
Re: Dennis Ritchie on the priorities of && || vs. == etc. (1982)
#20Earlier quoted context omitted.
a * b * c is only nonzero if all of a, b, c are nonzero. That's AND, and should be pretty intuitive. a + b + c is nonzero if any of them are nonzero. (Remember each value is either 0 or 1.) So that's the intuition for OR.
a+b+c is nonzero if any of a or b or c are nonzero.