Logical operators priority with NAND, NOR, XNOR

眉间皱痕 提交于 2019-12-19 06:19:08

问题


I've searched the web but I've found no solution to this problem.

What is the logical priority for operators NAND, NOR and XNOR?

I mean, considering as example the expression

A AND B NAND C

which operator should be evaluated first?
Obviously NAND can be translated as NOT-AND (as NOR is NOT-OR and XNOR is NOT-XOR), but

(A AND B) NAND C != A AND (B NAND C) = A AND NOT(B AND C)

According to my researches there's no a defined priority for such an expression, so I think the simplest solution is to evaluate the operators according to the order they appear in the expression, but I may be wrong.

Any suggestions?


回答1:


This actually depends on your precedence rules. If there is no order (no precedence rules or everything of the same importance), it should be solved left to right. Here is an example with C++.




回答2:


operator precedence have to be defined by a language, and what you have here doesn't seem to be a formal language, in such cases it's often assumed to be evaluated as you read from left to right.

Though, you could use the same operator precedence as verilog , or look at wikipedia which has a small table precedence commonly used for logic operators




回答3:


If the expression is written like the way it is mentioned in the question(without brackets in between), it should be solved in the order they are written. Thats the only correct way to do this. eg. If its written line A NOR B XOR C, It simply means (A NOR B) XOR C




回答4:


Boolean operators have analogues in conventional arithmetic, so one way of deciding what the precedence rules should be is to follow the rules for conventional arithmetic, e.g. AND is analogous to multiplication, while OR is analogous to addition, hence AND should have higher precedence than OR. If you look at the operator precedence table for a language such as C or C++ you will see that this is indeed the case in these and other related languages.




回答5:


I suppose this could be language specific, ALL operators must have an order of precedence defined or implied by a specific implmentation.

Here is what another site has to say about that.



来源:https://stackoverflow.com/questions/8167779/logical-operators-priority-with-nand-nor-xnor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!