In most programming languages (C#, JavaScript, Java) the order of operations precedence has that equality comparison come BEFORE bitwise comparisons.
This means that if you have a bit operation and an equality comparison on the same line (as in this javascript code):
if(2 & 15 === 2){
//do something
}
You will get an unexpected result (it evaluates to 0 in JavaScript). C# throws an error if you try to do this and won't compile. Java, presumably, throws an error as well (didn't fire it up to check).
It seems to me that the errors and the confusion in JavaScript could be removed completely by simply putting the Equality operators at a lower precedence than the Bitwise operators.
I can't think of an instance where I want to have both a bitwise and an equality operation on the same line AND want to have the equality evaluated first.
Yet every programming language that I've checked shares the same order of operations.
What is the reason for this? Is there a common, legitimate, use that I am missing that makes it easier to have the precedence as designed?
if(a == 2 && b == 2)
. The bitwise operators do seem a bit silly.errors and the confusion ... could be removed
: The way to get rid of errors and confusion is for the programmer to use parentheses to make his intent unambiguous, e.g.,((2 & 15) === 2)
.