除了 AND/OR/NOT 之外,编程中其他逻辑运算符的意义是什么?
我几乎一生都在编程(大约 20 多年),我不记得有一次当我看着 if 语句时会想“嗯,这将是一个好时机使用异或。”整个逻辑编程世界似乎只围绕着这三个。
当然,使用 AND/OR/NOT 门,您可以做出任何其他逻辑语句。但是,有时可能会节省一些将两个或三个语句组合成单个逻辑语句的代码。让我们看一下逻辑连接词的 16 种可能的组合:
- FALSE = Contradiction = 0, null, NOT TRUE
- TRUE = 同义反复 = 1,NOT FALSE
- X = 命题 X = X
- NOT X = X 的否定 = !X
- Y = 命题 Y = Y
- NOT Y = Y 的否定 = !Y
- X AND Y = 合取 = NOT (X NAND Y)
- X NAND Y = 替代否定 = NOT (X AND Y), !X OR !Y
- X OR Y = 析取 = NOT ( !X AND !Y)
- X NOR Y = 联合否认 = NOT (X OR Y), !X AND !Y
- X ⊅ Y = 实质性非暗示 = X AND !Y, NOT(!X OR Y), (X XOR Y) 和 X,???
- X ⊃ Y = 实质性含义 = !X OR Y, NOT(X AND !Y), (X XNOR Y) OR X, ???
- X ⊄ Y = 逆非蕴涵 = !X AND Y, NOT(X OR !Y), (X XOR Y) AND Y, ???
- X ⊂ Y = 逆蕴涵 = X OR !Y, NOT(!X AND Y), (X XNOR Y) OR Y, ???
- X XOR Y = 异或 = NOT (X IFF Y), NOT (X XNOR Y), X != Y
- X XNOR Y = 双条件 = X IFF Y, NOT (X XOR Y), !X AND !Y
因此,项目第1-2项涉及零个变量,第3-6项涉及1个变量,第7-10项是我们熟悉的术语。 (虽然,我们通常没有 NAND 运算符,但至少 Perl 有“除非”表示通用 NOT。)
第 11-14 项看起来很有趣,但我从未在编程中见过这些。第 15-16 项是 XOR/XNOR。
其中任何一个都可以用于 AND/OR/NOT 简化吗?如果是的话,你用过它们吗?
更新:“不等于”或!=实际上是异或,经常使用。所以,XOR 终究还是被使用了。
I've been programming nearly all of my life (around 20+ years), and I don't think I can remember a single time when I was looking at a if-statement and think "Hmmm, this would be a good time to use XOR." The entire logical programming universe seems to revolve around just these three.
Granted, with AND/OR/NOT gates, you can make any other logical statement. However, there might be a time where it might save you some code to combine two or three statements into a single logical statement. Let's look at the 16 possible combinations of logical connectives:
- FALSE = Contradiction = 0, null, NOT TRUE
- TRUE = Tautology = 1, NOT FALSE
- X = Proposition X = X
- NOT X = Negation of X = !X
- Y = Proposition Y = Y
- NOT Y = Negation of Y = !Y
- X AND Y = Conjunction = NOT (X NAND Y)
- X NAND Y = Alternative Denial = NOT (X AND Y), !X OR !Y
- X OR Y = Disjunction = NOT (!X AND !Y)
- X NOR Y = Joint Denial = NOT (X OR Y), !X AND !Y
- X ⊅ Y = Material Nonimplication = X AND !Y, NOT(!X OR Y), (X XOR Y) AND X, ???
- X ⊃ Y = Material Implication = !X OR Y, NOT(X AND !Y), (X XNOR Y) OR X, ???
- X ⊄ Y = Converse Nonimplication = !X AND Y, NOT(X OR !Y), (X XOR Y) AND Y, ???
- X ⊂ Y = Converse Implication = X OR !Y, NOT(!X AND Y), (X XNOR Y) OR Y, ???
- X XOR Y = Exclusive disjunction = NOT (X IFF Y), NOT (X XNOR Y), X != Y
- X XNOR Y = Biconditional = X IFF Y, NOT (X XOR Y), !X AND !Y
So, items 1-2 involve zero variables, items 3-6 involve one, and items 7-10 are terms we are familiar with. (Though, we don't usually have a NAND operator, but at least Perl has "unless" for universal NOT.)
Items 11-14 seem like interesting ones, but I've never seen these in programming. Items 15-16 are the XOR/XNOR.
Can any of these be used for AND/OR/NOT simplification? If so, have you used them?
UPDATE: "Not equal" or != is really XOR, which is used constantly. So, XOR is being used after all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在不等于/异或之后结束这个问题。在 16 个可能的运算符中,程序员使用了其中的 9 个:
所有其他运算符通常不存在于编程语言中:
也许以后会有它们的空间,因为 NAND/NOR 看起来非常方便,并且比键入 NOT (X xxx 是)。
Going to close this question after the Not Equals/XOR thing. Out of the 16 possible operators, programmers use 9 of them:
All of the other operators don't typically exist in programming languages:
Perhaps there's room for them later on, because NAND/NOR seems pretty handy, and cleaner than typing NOT (X xxx Y).
考虑一下:
使用 and/or/not,你可能会尝试
这非常难看,因为你最终必须指定每个 1-sets,3-sets,5-sets,...,2n+1 集,它们是 的子集您的条件集。不过,XOR 版本非常优雅......
对于一个大的或可变的 N,这可能最好用循环和计数器系统来处理,但是当 N 不太大(~10)时,并且您还没有将条件存储为数组,这还不错。检查偶数个条件的工作方式相同。
您也可以为其他人想出类似的例子。一个有趣的练习是尝试编程类似
And 看看编译器是否为 AND、OR 和 NOT 发出汇编语言,或者是否足够聪明来识别这是 XOR,并基于此发出(可能更便宜的)XOR 指令。
Consider this:
Using and/or/not, you might try
That's pretty ugly because you end up having to specify each of the 1-sets, 3-sets, 5-sets, ..., 2n+1 sets which are subsets of the set of your conditions. The XOR version is pretty elegant, though...
For a large or variable N, this is probably better handled with a loop and counter system anyway, but when N isn't too large (~10), and you aren't already storing the conditions as an array, this isn't so bad. Works the same way for checking an even number of conditions.
You can come up with similar examples for the others, too. An interesting exercise would be to try programming something like
And see whether the compiler emits assembly language for ANDs, ORs and NOTs or is smart enough to recognize this is XOR and, based on this, emits (a possibly cheaper) XOR instruction.
在用java编程时,我倾向于主要使用以下逻辑函数:
!
&&
||
==
!=
,将其扩展到其他基本函数:
A || !B
!A || B
!A && B
A && !B
了解何时使用 xor 和 xnor 可以归结为简化逻辑。一般来说,当您有一个复杂的函数时:
1) 简化为 CNF(“合取范式”又名“乘积求和”)或 DNF(“析取范式”又名“乘积求和”)。*
2) 删除多余的内容术语
A && (A || B)
,A || (A && B)
->A
2) 简化
(A || !B) && (!A || B)
,(!A && !B) || (A && B)
->A == B
3) 简化
(A || B) && (!A || !B)
,(A && !B) || (!A && B)
->A != B
使用这 3 种简化可以使使用 xor 和 xnor 函数的代码更加简洁。
*应该注意的是,DNF 中的逻辑函数可能比 CNF 简单得多,反之亦然。
When programming in java, I tend to mostly use the following logic functions:
!
&&
||
==
!=
,extending this to other basic functions:
A || !B
!A || B
!A && B
A && !B
Knowing when to use the xor and xnor comes down to simplifying the logic. In general, when you have a complex function:
1) simplify to either CNF ("conjunctive normal form" aka "sum over product") or DNF ("disjunctive normal form" aka "product over sum").*
2) remove extra terms
A && (A || B)
,A || (A && B)
->A
2) simplify
(A || !B) && (!A || B)
,(!A && !B) || (A && B)
->A == B
3) simplify
(A || B) && (!A || !B)
,(A && !B) || (!A && B)
->A != B
Using these 3 simplifications can lead to much cleaner code using both the xor and xnor functions.
*It should be noted that a logical function may be much simpler in DNF than CNF or vice versa.
“第 11-14 项看起来很有趣,但我从未在编程中见过这些。”
我不同意。第12项,Material Implication基本上是一个“IF”语句,它在编程中无处不在。
我认为实质性含义与以下内容相同:
"Items 11-14 seem like interesting ones, but I've never seen these in programming."
I disagree. item 12, Material Implication is basically a "IF" statement, it is everywhere in programming.
I see Material Implication same as:
材料非暗示/放弃用例
现在有一个实例,我想在其中执行材料非暗示/放弃。
真值表
我正在同时处理多个实体的多个权限(幸运的是
true
/false
),并且有一个我想要的角色和权限情况查看系统用户是否可以更改另一个用户的权限。同时对所有实体尝试相同的操作。首先,我想要实体的旧权限状态与所有实体新的通常所需权限状态之间的增量。
然后我想将该增量与当前用户对该特定实体的更改权限进行比较。
true
标志。示例
现在我只是执行一个
XOR
,然后执行一个AND
,这是同一件事。哪种代码闻起来有一种更简单的方法来进行比较,但至少在这种令人难以置信的乏味、逐步的逻辑中,拥有该运算符会很好。
Material nonimplication/abjunction use case
Got an instance now where I'd like to do a material nonimplication/abjunction.
Truth Table
I'm dealing with a number of permissions (all luckily
true
/false
) for a number of entities at once and have a roles and rights situation where I want to see if a system user can change another user's permissions. The same operation is being attempted on all the entities at once.First I want the delta between the between an entity's old permission state and new commonly desired permission state for all entities.
Then I want to compare that delta to the current user's change rights for this specific entity.
true
flag where an action should be blocked.Example
Right now I'm just doing an
XOR
and then anAND
, which is the same thing.Which kinda code smells that there's an easier way to do the comparison, but at least in this incredibly stodgy, step-by-step logic, it would be nice to have that operator.