Bitwise & vs Logical && Operators – Bitwise & vs Logical && 操作符

最后修改: 2019年 12月 25日

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

1. Introduction

1.绪论

In Java, we’ve got two ways to say “AND”. But which to use?

在Java中,我们有两种方法来表示 “和”。但是用哪一种呢?

In this tutorial, we’ll look at the differences between & and &&. And, we’ll learn about bitwise operations and short-circuiting along the way.

在本教程中,我们将看看&和&的区别。此外,我们还将学习位操作和短路的相关知识。

2. Use of Bitwise AND

2.使用位法AND

The bitwise AND (&) operator compares each binary digit of two integers and returns 1 if both are 1, otherwise, it returns 0. 

位数与(&)运算符比较两个整数的每个二进制数字,如果都是1,则返回1,否则返回0。

Let’s take a look at two integers:

让我们看一下两个整数。

int six = 6;
int five = 5;

Next, let’s apply a bitwise AND operator on these numbers:

接下来,让我们对这些数字应用一个位数和运算符。

int resultShouldBeFour = six & five;
assertEquals(4, resultShouldBeFour);

To understand this operation, let’s look at the binary representation of each number:

为了理解这个操作,让我们看看每个数字的二进制表示。

Binary of decimal 4: 0100
Binary of decimal 5: 0101
Binary of decimal 6: 0110

The & operator performs a logical AND on each bit, and returns a new binary number:

&运算符对每个位进行逻辑和,并返回一个新的二进制数字。

0110
0101
-----
0100

Finally, our result –  0100 – can be converted back to decimal number – 4.

最后,我们的结果–0100–可以转换回十进制数字–4

Let’s see the test Java code:

让我们看看测试的Java代码。

int six = 6;
int five = 5;
int resultShouldBeFour = six & five;
assertEquals(4, resultShouldBeFour);

2.1. Use of & with Booleans

2.1.与布尔运算有关的&的使用

Also, we can use the bitwise AND (&) operator with boolean operands. It returns true only if both the operands are true, otherwise, it returns false.

此外,我们还可以使用操作数为&的bitwise AND (&)运算符,boolean操作数。只有当操作数都是true时,它才会返回true,否则就会返回false。

Let’s take three boolean variables:

让我们取三个boolean变量。

boolean trueBool = true;
boolean anotherTrueBool = true;
boolean falseBool = false;

Next, let’s apply a bitwise AND operator on variables trueBool and anotherTrueBool:

接下来,让我们对变量trueBoolanotherTrueBool应用一个位数和运算符。

boolean trueANDtrue = trueBool & anotherTrueBool;

Then, the result will be true.

那么,结果将是

Next, let’s apply a bitwise AND operator on trueBool and falseBool:

接下来,让我们对trueBoolfalseBool应用一个位数和运算符。

boolean trueANDFalse = trueBool & falseBool;

In this case, the result will be false.

在这种情况下,结果将是false

Let’s see the test Java code:

让我们看看测试的Java代码。

boolean trueBool = true;
boolean anotherTrueBool = true;
boolean falseBool = false;

boolean trueANDtrue= trueBool & anotherTrueBool;
boolean trueANDFalse = trueBool & falseBool;

assertTrue(trueANDtrue);
assertFalse(trueANDFalse);

3. Use of Logical AND

3.使用合乎逻辑的and

Like &, the logical AND (&&) operator compares the value of two boolean variables or expressions. And, it returns also true only if both operands are true, otherwise, it returns false.

&一样,逻辑和(&&)运算符对两个布尔变量或表达式的值进行比较。而且,只有当两个操作数都是时,它才会返回,否则,它会返回

Let’s take three boolean variables:

让我们取三个boolean变量。

boolean trueBool = true;
boolean anotherTrueBool = true;
boolean falseBool = false;

Next, let’s apply a logical AND operator on variables trueBool and anotherTrueBool:

接下来,让我们对变量trueBoolanotherTrueBool应用逻辑和运算符。

boolean trueANDtrue = trueBool && anotherTrueBool;

Then, the result will be true.

那么,结果将是

Next, let’s apply a logical AND operator on trueBool and falseBool:

接下来,让我们在trueBoolfalseBool上应用一个逻辑和运算符。

boolean trueANDFalse = trueBool && falseBool;

In this case, the result will be false.

在这种情况下,结果将是false

Let’s see the test Java code:

让我们看看测试的Java代码。

boolean trueBool = true;
boolean anotherTrueBool = true;
boolean falseBool = false;
boolean anotherFalseBool = false;

boolean trueANDtrue = trueBool && anotherTrueBool;
boolean trueANDFalse = trueBool && falseBool;
boolean falseANDFalse = falseBool && anotherFalseBool;

assertTrue(trueANDtrue);
assertFalse(trueANDFalse);
assertFalse(falseANDFalse);

3.1. Short-circuit

3.1 短路

So, what’s the difference? Well, the && operator short-circuits. This means it doesn’t evaluate the right-hand side operand or expression when the left-hand side operand or expression is false.

那么,有什么区别呢?嗯,&&操作符会短路。这意味着当左边的操作数或表达式为false时,它不会评估右边的操作数或表达式。

Let’s take two expressions evaluating as false:

让我们来看看两个表达式的评估结果为假。

First Expression: 2<1
Second Expression: 4<5

When we apply a logical AND operator on expressions 2<1 and 4<5, then it evaluates only the first expression 2<1 and returns false.

当我们在表达式2<14<5上应用逻辑和运算符时,那么它只评估第一个表达式2<1并返回假。

boolean shortCircuitResult = (2<1) && (4<5);
assertFalse(shortCircuitResult);

3.2. Use of && with Integers

3.2.整数的&&的使用

We can use the & operator with boolean or numeric types but && can only be used with boolean operands. Using it with integer operands results in a compilation error:

我们可以在布尔型或数字型中使用&运算符,但&&只能在布尔型操作数中使用。在整数操作数上使用它,会导致编译错误。

int five = 2;
int six = 4;
int result = five && six;

4. Comparison

4.比较

  1. The & operator always evaluates both expressions, whereas the && operator evaluates the second expression only if the first one is true
  2. & compares each operand bitwise, whereas && operates only on booleans

5. Conclusion

5.总结

In this article, we used the bitwise & operator to compare bits of two digits resulting in a new digit. Also, we used the logical && operator to compare two booleans, resulting in a boolean value.

在这篇文章中,我们用位数&运算符来比较两个数字的位数,结果是一个新的数字。此外,我们还使用了逻辑&&运算符来比较两个布尔值,得出一个布尔值。

We also saw some key differences between the two operators.

我们也看到了两个运营商之间的一些关键差异。

As always you can find the code for this tutorial over on GitHub.

像往常一样,你可以在GitHub上找到本教程的代码over