The XOR Operator in Java – Java中的XOR操作符

最后修改: 2019年 8月 28日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll learn about the Java XOR operator. We’ll discuss a bit of theory about XOR operations, and then we’ll see how to implement them in Java.

在这个快速教程中,我们将学习Java的XOR操作。我们将讨论一些关于XOR操作的理论,然后我们将看到如何在Java中实现它们。

2. The XOR Operator

2.XOR运算符

Let’s begin with a reminder of the semantics of the XOR operation. The XOR logical operation, exclusive or, takes two boolean operands and returns true if, and only if, the operands are different. Conversely, it returns false if the two operands have the same value.

首先,让我们提醒一下XOR操作的语义。XOR逻辑操作,exclusive or,接收两个布尔操作数,如果且仅当操作数不同时返回真。反之,如果两个操作数的值相同,则返回假。

So, for example, the XOR operator can be used when we have to check for two conditions that can’t be true at the same time.

因此,例如,当我们要检查两个不可能同时为真的条件时,可以使用XOR操作符。

Let’s consider two conditions, A and B. The following table shows the possible values of A XOR B:

让我们考虑两个条件,AB。 下表显示了A XOR B的可能值。

XOR Operator Table

XOR运算符表

The A XOR B operation is equivalent to (A AND !B) OR (!A AND B). Parentheses have been included for clarity, but are optional, as the AND operator takes precedence over the OR operator.

A XOR B操作等同于(A AND !B) OR (!A AND B)为了清晰起见,我们加入了括号,但这是可选的,因为AND操作符优先于OR操作符。

3. How to Do It in Java?

3.如何在Java中做到这一点?

Now let’s see how to express the XOR operation in Java. Of course, we have the option to use the && and || operators, but this can be a bit wordy, as we’re going to see.

现在让我们看看如何用Java来表达XOR操作。当然,我们可以选择使用&&||操作符,但这可能有点啰嗦,我们将看到。

Imagine a Car class having two boolean attributes: diesel and manual. Now let’s say we want to tell if the car is either diesel or manual, but not both.

想象一下,一个Car类有两个boolean属性。dieselmanual。现在我们假设我们想知道这辆车是柴油的还是手动的,但不是两者都是。

Let’s check this using the && and || operators:

让我们用&&||运算符来检查。

Car car = Car.dieselAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());

That’s a bit long, especially considering that we have an alternative, the Java XOR operator represented by the ^ symbol. It’s a bitwise operator, meaning it’s an operator comparing the matching bits of two values in order to return a result. In the XOR case, if two bits of the same position have the same value, the resulting bit will be 0. Otherwise, it’ll be 1.

这有点长,特别是考虑到我们有一个替代方案,即由^符号代表的Java XOR运算符。这是一个bitwise运算符,意味着它是一个比较两个值的匹配位以返回一个结果的运算符。在XOR的情况下,如果相同位置的两个位具有相同的值,那么结果位将是0。 否则,它将是1。

So instead of our cumbersome XOR implementation, we can use the ^ operator directly:

因此,我们可以直接使用^运算符,而不是我们繁琐的XOR实现。

Car car = Car.dieselAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();

As we can see, the ^ operator allows us to be more concise in expressing XOR operations.

我们可以看到,^操作符允许我们更简洁地表达XOR操作。

Finally, it’s worth mentioning that the XOR operator, like the other bitwise operators, works with every primitive type. For example, let’s consider two integers, 1 and 3, whose binary representations are 00000001 and 000000011, respectively. Using the XOR operator between them will result in the integer 2:

最后,值得一提的是,XOR操作符,就像其他位操作符一样,适用于每个原始类型。例如,让我们考虑两个整数,1和3,其二进制表示分别为00000001和0000011。在它们之间使用XOR运算符将产生整数2。

assertThat(1 ^ 3).isEqualTo(2);

Only the second bit is different in those two numbers; therefore, the result of the XOR operator on this bit will be 1. All other bits are identical, so their bitwise XOR result is 0, giving us a final value of 00000010, the binary representation of the integer 2.

这两个数字中只有第二位是不同的,因此,这个位上的XOR运算结果将是1。所有其他位都是相同的,所以它们的位数XOR结果是0,给我们一个最终值000010,整数2的二进制表示。

4. Conclusion

4.总结

In this article, we learned about the Java XOR operator. We demonstrated how it offers a concise way to express XOR operations.

在这篇文章中,我们了解了Java的XOR操作符。我们演示了它是如何提供一种简洁的方式来表达XOR运算的。

As usual, the full code of the article can be found over on GitHub.

像往常一样,文章的完整代码可以在GitHub上找到over