条件异或?

发布于 2024-11-17 16:07:15 字数 136 浏览 7 评论 0原文

为什么 C# 没有条件 XOR 运算符?

例子:

true  xor false = true
true  xor true  = false
false xor false = false

How come C# doesn't have a conditional XOR operator?

Example:

true  xor false = true
true  xor true  = false
false xor false = false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

时光倒影 2024-11-24 16:07:16

条件异或应该像这样工作:

true xor false = true
true xor true = false
false xor true = true
false xor false = false

但这就是 != 运算符实际与 bool 类型一起工作的方式:

(true != false) // true
(true != true) // false
(false != true) // true
(false != false) // false

因此,如您所见,不存在的 ^^ 可以替换为现有的 <代码>!=。

Conditional xor should work like this:

true xor false = true
true xor true = false
false xor true = true
false xor false = false

But this is how the != operator actually works with bool types:

(true != false) // true
(true != true) // false
(false != true) // true
(false != false) // false

So as you see, the nonexistent ^^ can be replaced with existing !=.

同尘 2024-11-24 16:07:16

在 C# 中,条件运算符仅在必要时才执行其辅助操作数。

由于 XOR 根据定义必须测试两个值,因此条件版本将是愚蠢的。

示例

  • 逻辑 AND:& - 每次都测试双方。

  • 逻辑或:| - 每次都测试双方。

  • 条件 AND:&& - 仅在第 1 边为 true 时测试第 2 边。

  • 条件 OR:|| - 如果第 1 边为 false,则仅测试第 2 边。

In C#, conditional operators only execute their secondary operand if necessary.

Since an XOR must by definition test both values, a conditional version would be silly.

Examples:

  • Logical AND: & - tests both sides every time.

  • Logical OR: | - test both sides every time.

  • Conditional AND: && - only tests the 2nd side if the 1st side is true.

  • Conditional OR: || - only test the 2nd side if the 1st side is false.

疯到世界奔溃 2024-11-24 16:07:16

有逻辑 XOR 运算符:^

文档:C# 运算符^ 运算符

文档明确指出 ^ 在与布尔操作数一起使用时是布尔运算符。

“对于 bool 操作数,^ 运算符计算的结果与
不等式运算符 !=".

(正如另一个答案中所述,这正是您想要的)。

您也可以按位-与 ^ 异或整数操作数。

There is the logical XOR operator: ^

Documentation: C# Operators and ^ Operator

The documentation explicitly states that ^, when used with boolean operands, is a boolean operator.

"for the bool operands, the ^ operator computes the same result as the
inequality operator !=".

(And as noted in another answer, that's exactly what you want).

You can also bitwise-xor integer operands with ^.

伴随着你 2024-11-24 16:07:16

需要澄清的是,^ 运算符适用于整型和布尔类型。

请参阅MSDN 的 ^ 运算符(C# 参考)

二元 ^ 运算符是为整数类型和布尔值预定义的。对于整型,^ 计算其操作数的按位异或。对于 bool 操作数,^ 计算其操作数的逻辑异或;也就是说,当且仅当其操作数之一为 true 时,结果才为 true。

也许自 2011 年提出这个问题以来,文档已经发生了变化。

Just as a clarification, the ^ operator works with both integral types and bool.

See MSDN's ^ Operator (C# Reference):

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

Maybe the documentation has changed since 2011 when this question was asked.

土豪我们做朋友吧 2024-11-24 16:07:16

正如 Mark L 所问,以下是正确的版本:

 Func<bool, bool, bool> XOR = (X,Y) => ((!X) && Y) || (X && (!Y));

这是真值表:

 X | Y | Result
 ==============
 0 | 0 | 0
 1 | 0 | 1
 0 | 1 | 1
 1 | 1 | 0

参考:
异或

As asked by Mark L, Here is the correct version:

 Func<bool, bool, bool> XOR = (X,Y) => ((!X) && Y) || (X && (!Y));

Here is the truth table:

 X | Y | Result
 ==============
 0 | 0 | 0
 1 | 0 | 1
 0 | 1 | 1
 1 | 1 | 0

Reference:
Exclusive OR

旧时光的容颜 2024-11-24 16:07:16

哦,是的,确实如此。

bool b1 = true;
bool b2 = false;
bool XOR = b1 ^ b2;

Oh yes, it does.

bool b1 = true;
bool b2 = false;
bool XOR = b1 ^ b2;
没企图 2024-11-24 16:07:16

条件异或不存在,但您可以使用逻辑异或,因为异或是为布尔值定义的,并且所有条件比较都会计算为布尔值。

所以你可以这样说:

if ( (a == b) ^ (c == d))
{

}

Conditional xor doesn't exist, but you can use logical one because xor is defined for booleans, and all conditional comparisons evaluate to booleans.

So you can say something like:

if ( (a == b) ^ (c == d))
{

}
峩卟喜欢 2024-11-24 16:07:16

continue

While there is a logical xor operator ^, there is no conditional xor operator. You can achieve a conditional xor of two values A and B using the following:

A ? (!B) : B

The parens are not necessary, but I added them for clarity.

As pointed out by The Evil Greebo, this evaluates both expressions, but xor cannot be short circuited like and and or.

貪欢 2024-11-24 16:07:16

你可以使用:

a = b ^ c;

就像在c/c++中一样

you can use:

a = b ^ c;

just like in c/c++

烂人 2024-11-24 16:07:16

continue

There is no such thing as conditional (short-circuiting) XOR. Conditional operators are only meaningful when there's a way to definitively tell the final outcome from looking at only the first argument. XOR (and addition) always require two arguments, so there's no way to short-circuit after the first argument.

If you know A=true, then (A XOR B) = !B.

If you know A=false, then (A XOR B) = B.

In both cases, if you know A but not B, then you don't know enough to know (A XOR B). You must always learn the values of both A and B in order to calculate the answer. There is literally no use case where you can ever resolve the XOR without both values.

Keep in mind, XOR by definition has four cases:

false xor true  = true
true  xor false = true
true  xor true  = false
false xor false = false

Again, hopefully it's obvious from the above that knowing the first value is never enough to get the answer without also knowing the second value. However, in your question, you omitted the first case. If you instead wanted

false op true  = false (or DontCare)
true  op false = true
true  op true  = false
false op false = false

then you can indeed get that by a short-circuiting conditional operation:

A && !B

But that's not an XOR.

夜未央樱花落 2024-11-24 16:07:16

continue

NOTE: I know XOR and XNOR are bitwise operations, but considering the thing we are questioning here...

Ain't this work as conditional (boolean) XOR?

bool Xor(bool a, bool b){ return a != b }
abx
FalseFalseFalse
FalseTrueTrue
TrueFalseTrue
TrueTrueFalse

Also I believe you can loop though data, aggregate them to use more than two operator. and still get the same result as Bitwise of Nth operand in same conditional manner

流绪微梦 2024-11-24 16:07:16

这个问题已经得到了有效的回答,但我遇到了不同的情况。确实不需要条件异或。确实可以使用 ^ 运算符。但是,如果您只需要测试操作数的“true || false”状态,那么 ^ 可能会导致麻烦。例如:

void Turn(int left, int right)
{
    if (left ^ right)
    {
        //success... turn the car left or right...
    }
    else
    {
        //error... no turn or both left AND right are set...
    }
}

在此示例中,如果 left 设置为 10 (0xa),right 设置为 5 (0x5),则进入“成功”分支。对于这个(简单但愚蠢)的示例,这会导致错误,因为您不应该同时向左和向右转。我从提问者那里得到的信息并不是他实际上想要一个条件,而是一种对传递给异或的值执行真/假的简单方法。

一个宏可以做到这一点:

#define my_xor(a, b) ( ((a)?1:0) ^ ((b)?1:0) )

如果我不合时宜,请随意打我一巴掌:o)

在我发布这个之后,我读了下面吉姆里德的答案(坏Yapdog!),他的答案实际上更简单。这会起作用,我完全不知道为什么他的答案被否决了......

This question has been affectively answered, but I came across a different situation. It's true that there is no need for a conditional XOR. It's also true that the ^ operator can be used. However, if you need to only test the "true || false" status of the operands then ^ can lead to trouble. For example:

void Turn(int left, int right)
{
    if (left ^ right)
    {
        //success... turn the car left or right...
    }
    else
    {
        //error... no turn or both left AND right are set...
    }
}

In this example, if left is set to 10 (0xa) and right is set to 5 (0x5) the "success" branch is entered. For this (simplistic if silly) example, this would result in a bug since you shouldn't turn left AND right at the same time. What I gathered from the questioner is not that he actually wanted a conditional, but a simple way to perform the true/false on the values as passed to the xor.

A macro could do the trick:

#define my_xor(a, b) ( ((a)?1:0) ^ ((b)?1:0) )

Feel free to slap me around if I'm off the mark :o)

I read jimreed's answer below after I posted this (bad Yapdog!) and his is actually simpler. It would work and I have absolutely no idea why his answer was voted down...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文