Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `1 XOR 1 OR 1` ambiguous in Boolean algebra?

Using boolean algebra (not a specific language implementation), can we evaluate 1 ^ 1 + 1 (or 1 XOR 1 OR 1) unambiguously?

I can derive two evaluations:

 [I]:  (1 ^ 1) + 1 = 0 + 1 = 1
[II]:  1 ^ (1 + 1) = 1 ^ 1 = 0

Perhaps there's some stated order of operations, or of a left-to-right evaluation? Or is this not defined in Boolean algebra?

like image 947
Cannoliopsida Avatar asked Dec 11 '25 19:12

Cannoliopsida


2 Answers

We can use the rules of boolean algebra to attempt to evaluate the expression 1 XOR 1 OR 1.

Now:

  • XOR is derived from OR such that A XOR B = (¬A AND B) OR (¬B AND A);
  • Associativity tells us that A OR (B OR C) = (A OR B) OR C;
  • Associativity also tells us that A AND (B AND C) = (A AND B) AND C

So, taking either of the possible interpretations of evaluation order:

  (1 XOR 1) OR 1                       1 XOR (1 OR 1)

Even though we have no left-to-right "evaluation order" defined, these rules are all we need to show that the two possible interpretations are not equivalent:

= (¬1 AND 1) OR (¬1 AND 1) OR 1      = (¬1 AND (1 OR 1)) OR (¬(1 OR 1) AND 1)
= (0 AND 1) OR (0 AND 1) OR 1        = (0 AND 1) OR (0 AND 1)
= 0 OR 0 OR 1                        = 0 OR 0
= 1                                  = 0

Unless I'm forgetting some crucially pertinent axiom, I've confirmed that you need more context to evaluate the given expression.

(And, of course, examining the expression A XOR B OR CA,B,C is of course substantially more tricky! But if the expression is ambiguous for just one value of all three inputs, then why bother checking for any others?)

This context is usually provided in language-specific evaluation-order rules. C-type languages give XOR a low precedence (something Richie came to dislike); by contrast, mathematical convention dictates a left-to-right associativity where no other axiom can be applied and an ambiguity is otherwise present.

So, basically, since we're ignoring language-specific rules, you'd probably go with [I].

like image 132
Lightness Races in Orbit Avatar answered Dec 14 '25 02:12

Lightness Races in Orbit


Most languages would do XOR then OR; experienced coders will put parentheses in anyway just to make the intent clear.

Many more modern languages do what's called fast- or short-circuit- evaluation, so 0 & ? is always 0, so ? will not be evaluated; same with 1 + ?.

like image 23
Tony Hopkinson Avatar answered Dec 14 '25 02:12

Tony Hopkinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!