Reputation: 7416
I'm writing a shunting yard algorithm in Javascript for boolean logic, and I'm running into a hitch with order of operations. The operations that I allow are:
and, or, implies, equals(biconditional), not, xor, nor, nand
However, I don't know what the precedence is for these. As of now, I have:
not>equals>implies>xor>nor>nand>or>and
Is this correct? Is there any standard I can use, similar to the PEMDAS/BODMAS system for numbers?
Upvotes: 3
Views: 2113
Reputation: 24976
The reason you are having such a hard time finding a definition of precedence of those operators for JavaScript is that:
and &
or |
implies →
equals (biconditional) ↔
not !
xor ⊕
nor ⊽
nand ⊼
From "Foundations of Computer Science" by Jeffrey D. Ullman
Associativity and Precedence of Logical Operators
The order of precedence we shall use is
1. NOT (highest)
2. NAND
3. NOR
4. AND
5. OR
6. IMPLIES
7. BICONDITIONAL(lowest)
From Mathematica
NOT
AND
NAND
XOR
OR
NOR
EQUIVALENT
IMPLIES
Upvotes: 3
Reputation: 1487
It's seems there is no a standart.
I've a book (Digital design by Morris Mano) that says NOT>AND>OR
. this is an accepted opinion.
About the rest, i've found few opinions.
This guy think EQUIV
is the lowest (Wikipedia assist). but this guy think EQUIV
is in the middle XOR>EQUIV>OR
(with few references).
Another disagreement is about XOR
place. here this third guy agree with the second guy :)
In short, two opinions:
1) NOT>AND>NAND>XOR>EQUIV>OR>NOR
(ignoring NOR
)
2) NOT>AND>NAND>NOR>OR>IMPLIES>XOR>EQUIV
Note: only the NOT>AND>OR
part is certified academically.
Upvotes: 0