逻辑运算符优先级为 NAND、NOR、XNOR
我在网上搜索过,但没有找到解决这个问题的方法。
运算符 NAND
、NOR
和 XNOR
的逻辑优先级是什么?
我的意思是,以表达式为例,
A AND B NAND C
应该首先评估哪个运算符?
显然 NAND
可以翻译为 NOT-AND
(因为 NOR
是 NOT-OR
和 XNOR< /code> 是
NOT-XOR
),但是
(A AND B) NAND C != A AND (B NAND C) = A AND NOT(B AND C)
根据我的研究,这样的表达式没有定义的优先级,所以我认为最简单的解决方案是根据运算符在表达式中出现的顺序来计算它们表达,但我可能是错的。
有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这实际上取决于您的优先规则。如果没有顺序(没有优先规则或所有内容都具有相同重要性),则应从左到右解决。 这里是一个 C++ 示例。
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++.
运算符优先级必须由语言定义,而您这里所拥有的似乎不是正式语言,在这种情况下,通常假设您从左到右阅读时对其进行评估。
不过,您可以使用与 相同的运算符优先级verilog ,或查看 wikipedia其表优先级较小,常用于逻辑运算符
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
如果表达式的书写方式与问题中提到的方式相同(中间没有括号),则应按照它们的书写顺序进行求解。这是执行此操作的唯一正确方法。
例如。如果写成
A NOR B XOR C
,它的意思就是(A NOR B) XOR C
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
布尔运算符在传统算术中具有类似物,因此决定优先规则的一种方法是遵循传统算术的规则,例如
AND
类似于乘法,而OR
类似于加法,因此AND
的优先级应高于OR
。如果您查看 C 或 C++ 等语言的运算符优先级表,您会发现这些语言和其他相关语言中确实存在这种情况。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, whileOR
is analogous to addition, henceAND
should have higher precedence thanOR
. 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.我想这可能是特定于语言的,所有运算符都必须具有由特定实现定义或暗示的优先顺序。
这里是另一个网站对此的说法。
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.