运营商^^() [英] operator^^()
问题描述
为什么没有逻辑xor运算符?
我常常在相互排斥的if语句中错过它。
-
弗拉基米尔
Why there is no logical xor operator?
I often miss it in mutually-exclusive if-statements.
--
Vladimir
推荐答案
*弗拉基米尔:
为什么没有逻辑xor运营商?
在[comp.std.c ++]中有更好的要求。
我经常在相互排斥的if语句中错过它。
Why there is no logical xor operator?
That''s better asked in [comp.std.c++].
I often miss it in mutually-exclusive if-statements.
您可以使用''!=''。当辩论者保证布尔值时你也可以使用''^''
。并且你可以使用功能表示法,
inline bool xor(bool a,bool b){return a ^ b; }
...
if(xor(5,0))...
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门帖子。
问:usenet和电子邮件中最烦人的事情是什么?
You can use ''!=''. When the argumens are guaranteed boolean you can also
use ''^''. And you can use functional notation,
inline bool xor( bool a, bool b ) { return a ^ b; }
...
if( xor( 5, 0 ) ) ...
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach写道:
Alf P. Steinbach wrote:
*弗拉基米尔:
* Vladimir:
为什么没有逻辑xor运算符?
Why there is no logical xor operator?
在[comp.std]中有更好的要求.c ++]。
That''s better asked in [comp.std.c++].
我经常在互相排斥的if语句中错过它。
I often miss it in mutually-exclusive if-statements.
你可以使用''!=''。当辩论者保证布尔值时你也可以使用''^''。你可以使用功能表示法,内联bool xor(bool a,bool b){return a ^ b; }
...如果(xor(5,0))...
You can use ''!=''. When the argumens are guaranteed boolean you can also
use ''^''. And you can use functional notation,
inline bool xor( bool a, bool b ) { return a ^ b; }
...
if( xor( 5, 0 ) ) ...
不能这样做。 " XOR"是一个关键字,不能用作函数
名称(2.11 / 2)。
V
Cannot do that. "xor" is a keyword, which cannot be used as a function
name (2.11/2).
V
>
Vladimir写道:
Vladimir wrote:
为什么没有逻辑xor运算符?
要计算XOR的结果,你必须评估两个操作数。
为了评估两个操作数之间的序列点,那里
已存在逗号运算符。
&&&和||是他们从左到右评估进行短路
。它们不仅仅是逻辑功能,而是
评估控制运营商。
我经常在相互排斥的if语句中错过它。
Why there is no logical xor operator?
To compute the result of XOR, you have to evaluate both operands.
For evaluating both operands with a sequence point in between, there
already exists the comma operator.
The virtue of && and || is that they perform short-circuited
left-to-right evaluation. They are not just logical functions, but
evaluation-controlling operators.
I often miss it in mutually-exclusive if-statements.
XOR没有你在测试相互
独有替代品时那么有用。
二进制XOR的结果,如果是真的,告诉你两个成分中只有一个是真的。但是三个或更多的b $ b成分之间的嵌套XOR并没有计算出来:它计算出这个条件为真的奇数
!
如果你有N个逻辑表达式,其中N> 2,你要测试
,其中一个是真的,你需要一个特别设计的,
变量参数XOR。
>
但请注意二进制和&&和||运营商确实以有用的方式结合起来。如果你有&&可能需要三个或更多操作数,它将相当于多个二进制和&&的嵌套。运营商。
换句话说:
A&& B&& C&& D $ />
做直观的事情。它一直在评估从左到右的条款
,直到它找到一个产生真实的条件。
写什么意思:
>
A ^^ B ^^ C ^^ D
必须评估所有条款:不短路。并且
如果这些条款中有任何奇数产生,则返回值为真。
为真:如果只有一个术语为真,或者任何三个是真的。
XOR is not as useful as you might think in testing for mutually
exclusive alternatives.
The result of a binary XOR, if true, tells you that exactly one of the
two constituents is true. But a nested XOR among three or more
constituents does not compute that: it computes that an odd number of
the terms is true!
If you have N logical expressions, where N > 2, and you want to test
that exactly one of them is true, you need a specially-designed,
variable-argument XOR.
But note that the binary && and || operators do combine in a useful
way. If you had an && that could take three or more operands, it would
be equivalent to a nesting of multiple binary && operators.
So in other words:
A && B && C && D
does the intuitive thing. It keeps evaluating the terms left to right
until it finds one that yields true.
What would it mean to write:
A ^^ B ^^ C ^^ D
It would have to evaluate all of the terms: no short circuiting. And
the return value would be true if any odd number of these terms yielded
true: if exactly one term is true, or if any three are true.
这篇关于运营商^^()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!