For multiple Booleans evaluation, is using && more efficient than ||?

  • A+

Assume we want to evaluate the Boolean value of (A || B || C) which is logically equal to !(!A && !B && !C).

Would it be more time-efficient to use !(!A && !B && !C) than the other if (!A) is false since the whole expression value becomes false? Will the program notice that? Or the program still needs to evaluate the value of (!B) and (!C) before returning the final result?

P.S. Just correcting the de'Morgan expressions and change from ~ to !. Thanks guys.

P.S.2 shown in this link by Retired Ninja and optimized version by David Foerster. The assembly code is actually the same!

(A || B || C) is logically equal to !(!A && !B && !C), you were missing the inner !s. The short circuiting in the second expression happens when !A is false, or equivalently when A is true. But when A is true, the first expression also short-circuits. In other words, these two expressions behave the same way with respect to short circuiting.

As Retired Ninja mentions in a comment, the two expressions produce the same assembly code, so there is no difference at all between them, except that one is harder to read.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: