Operator Precedence Pitfalls

Consider the following code:

It has 2 problems – both with assumptions on operator precedence:

the first one is the statement:

We maybe want it to be parsed as

but the priority of != is above | so we get it parsed as

this produced true (printing “yes”)

The second is:

we all know from first grade the multiply and divide precede plus and minus.

Also any developer knows the multiply by 8 is the same as 3 bits shifting

But … the precedence of bit shifting is bellow plus so it parses the statement as

 

Conclusion:

Always use parentheses , don’t trust operator precedence (even if you liked your first grade teacher)