AND OR order of operations

A good way to remember this is to think of it mathematically.

  • AND as * (multiply)
  • OR as + (addition)
  • TRUE as 1
  • FALSE as 0

So thinking of it as simple math you get this:

  • 0 * 0 = 0
  • 1 * 0 = 0
  • 1 * 1 = 1
  • 0 + 0 = 0
  • 1 + 0 = 1
  • 1 + 1 = 1

Only thing that may be a tiny bit confusing is 1 + 1 = 1, but a bit can't go above 1. But it makes sense if you think of 1 as any non-zero number.

So with this in mind you can then apply this logic:

if(cond1 AND cond2 AND cond3 OR cond4 AND cond5 AND cond6)

Becomes:

if(cond1 * cond2 * cond3 + cond4 * cond5 * cond6)

See: https://en.wikipedia.org/wiki/Order_of_operations


In most languages AND is evaluated first, hence

if((cond1 AND cond2 AND cond3) OR (cond4 AND cond5 AND cond 6))

is the right choice.

For C#, See http://msdn.microsoft.com/en-us/library/aa691323%28v=vs.71%29.aspx

For C, See http://en.cppreference.com/w/cpp/language/operator_precedence

For Java , See http://bmanolov.free.fr/javaoperators.php


In the normal set of boolean connectives (from a logic standpoint), and is higher-precedence than or, so A or B and C is really A or (B and C). Wikipedia lists them in-order. Most programming languages should obey this convention unless they are really weird.

That said, for your particular language or environment it should be possible to concoct a very small test to satisfy yourself that it is one way or the other :)