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?
We can use the rules of boolean algebra to attempt to evaluate the expression
1 XOR 1 OR 1.Now:
XORis derived fromORsuch thatA XOR B = (¬A AND B) OR (¬B AND A);A OR (B OR C) = (A OR B) OR C;A AND (B AND C) = (A AND B) AND CSo, taking either of the possible interpretations of evaluation order:
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:
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 C∀A,B,Cis 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
XORa 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].