Logical vs Bitwise OR Operator – 逻辑与位数OR运算法则

最后修改: 2021年 6月 26日

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

1. Introduction

1.绪论

In computer programming, the use case of OR is that it is either a logical construct for boolean logic or a bitwise mathematical operation for manipulating data at the bit level.

在计算机编程中,OR的使用情况是,它要么是布尔逻辑的逻辑结构,要么是在比特级操作数据的比特数学运算

The logical operator is used for making decisions based on certain conditions, while the bitwise operator is used for fast binary computation, including IP address masking.

逻辑运算符用于根据某些条件作出决定,而位运算符则用于快速二进制计算,包括IP地址屏蔽。

In this tutorial, we’ll learn about the logical and bitwise OR operators, represented by || and | respectively.

在本教程中,我们将学习逻辑运算符和位数运算符,分别用||和|表示

2. Use of Logical OR

2.使用逻辑OR

2.1. How It Works

2.1.它是如何工作的

The logical OR operator works on boolean operands. It returns true when at least one of the operands is trueotherwise, it returns false:

逻辑OR运算符对布尔操作数起作用。当操作数中至少有一个是true时,它返回true,否则,返回false

  • true || true = true
  • true || false = true
  • false || true = true
  • false || false = false

2.2. Example

2.2.例子

Let’s understand with the help of some boolean variables:

让我们在一些boolean变量的帮助下理解。

boolean condition1 = true; 
boolean condition2 = true; 
boolean condition3 = false; 
boolean condition4 = false;

When we apply logical OR on two true operands, the result will be true:

当我们对两个操作数应用逻辑OR时,结果将是

boolean result = condition1 || condition2;
assertTrue(result);

When we apply logical OR on one true and one false operand, the result will be true:

当我们对一个true和一个false操作数应用逻辑OR时,结果将是true

boolean result = condition1 || condition3; 
assertTrue(result);

And when we apply logical OR on two false operands, the result will be false:

而当我们对两个false操作数应用逻辑OR时,结果将是false

boolean result = condition3 || condition4; 
assertFalse(result);

When there are multiple operands, the evaluation is effectively performed from left to right. So, the expression condition1 || condition2 || condition3 || condition4 will result in the same logic as:

当有多个操作数时,评价实际上是从左到右进行的。因此,表达式condition1 || condition2 || condition3 || condition4 将导致相同的逻辑。

boolean result1 = condition1 || condition2; 
boolean result2 = result1 || condition3;
boolean finalResult = result2 || condition4;
assertTrue(finalResult);

In practice, though, Java can take a shortcut on the above expression.

不过在实践中,Java可以对上述表达式采取捷径。

3. Short-Circuit

3.短路

The logical OR operator has a short-circuit behaviour. This means it returns true as soon as one of the operands is evaluated as true, without evaluating the remaining operands.

逻辑OR运算符有一个短路行为。这意味着只要其中一个操作数被评估为true,它就会返回true,而不评估其余的操作数。

Let’s consider the following example:

让我们考虑下面的例子。

boolean returnAndLog(boolean value) { 
    System.out.println("Returning " + value); 
    return value; 
} 

if (returnAndLog(true) || returnAndLog(false)) { 
} 

Output:
Returning true

if (returnAndLog(false) || returnAndLog(true)) { 
}

Output:
Returning false
Returning true

Here we see that the second logical condition isn’t evaluated if an earlier condition is true.

这里我们看到,如果前面的条件是,第二个逻辑条件就不会被评估。

We should note that this can lead to unexpected results if any of the methods called have a side effect. We get a different result if we rewrite the first example to capture the boolean values before the if statement:

我们应该注意到,如果调用的任何方法有副作用,这可能会导致意外的结果。如果我们重写第一个例子,在if语句之前捕获boolean值,我们会得到不同的结果。

boolean result1 = returnAndLog(true);
boolean result2 = returnAndLog(false);

if (result1 || result2) {
}

Output:
Returning true
Returning false

4. Use of Bitwise OR

4.使用位数OR

4.1. How It Works

4.1.它是如何工作的

The bitwise OR is a binary operator and it evaluates OR of each corresponding bit of two integer operands. It returns 1 if at least one of the bits is 1, otherwise, it returns 0. Also, this operator always evaluates both the operands:

位数OR是一个二进制运算符,对两个整数操作数的每个对应位进行评估OR。如果至少有一个位是1,它就返回1,否则就返回0。另外,这个运算符总是对两个操作数进行评估。

  • 1 | 1 = 1
  • 1 | 0 = 1
  • 0 | 1 = 1
  • 0 | 0 = 0

So, when we apply the bitwise OR on two integers, the result will be a new integer.

因此,当我们在两个整数上应用位法OR时,结果将是一个新的整数。

4.2. Example

4.2.例子

Let’s consider an example:

让我们考虑一个例子。

int four = 4; //0100 = 4
int three = 3; //0011 = 3
int fourORthree = four | three;
assertEquals(7, fourORthree); // 0111 = 7

Now, we’ll look at how the above operation works.

现在,我们来看看上述操作是如何进行的。

First, each integer is converted to its binary representation:

首先,每个整数被转换为其二进制表示。

  • The binary representation of 4 is 0100
  • The binary representation of 3 is 0011

And then, the bitwise OR of the respective bits are evaluated to arrive at the binary representation that represents the final result:

然后,对各自的位进行评估,得出代表最终结果的二进制表示。

0100
0011
----
0111

Now, 0111, when converted back to its decimal representation, will give us the final result: the integer 7.

现在,0111,当转换为十进制表示时,我们将得到最终结果:整数7

When there are multiple operands, the evaluation is done from left to right. So, the expression 1 | 2 | 3 | 4 will be evaluated as:

当有多个操作数时,评估从左到右进行。因此,表达式1 | 2 | 3 | 4 将被评估为。

int result1 = 1 | 2; 
int result2 = result1 | 3;
int finalResult = result2 | 4;
assertEquals(finalResult,7);

5. Compatible Types

5.兼容类型

In this section, we’ll look at the data types that these operators are compatible with.

在这一节中,我们将看一下这些运算符所兼容的数据类型。

5.1. Logical OR

5.1.逻辑OR

The logical OR operator can only be used with boolean operands. And, using it with integer operands results in a compilation error:

逻辑OR运算符只能用于布尔操作数。而且,与整数操作数一起使用会导致编译错误。

boolean result = 1 || 2;

boolean result = 1 || 2;

Compilation error: Operator '||' cannot be applied to 'int', 'int

编译错误。操作符'||'不能应用于'int'、'int'

5.2. Bitwise OR

5.2 位数OR

Along with integer operands, the bitwise OR can also be used with boolean operands. It returns true if at least one of the operands is true, otherwise, it returns false.

除了整数操作数外,位法OR还可以用于布尔操作数。如果至少有一个操作数是true,它将返回true,否则,它将返回false

Let’s understand with the help of some boolean variables in an example:

让我们在一个例子中借助一些boolean变量来理解。

boolean condition1 = true;
boolean condition2 = true;
boolean condition3 = false;
boolean condition4 = false;
 
boolean condition1_OR_condition2 = condition1 | condition2;
assertTrue(condition1_OR_condition2);

boolean condition1_OR_condition3 = condition1 | condition3;
assertTrue(condition1_OR_condition3);

boolean condition3_OR_condition4 = condition3 | condition4;
assertFalse(condition3_OR_condition4);

6. Precedence

6.优先权

Let’s review the precedence of logical and bitwise OR operator, among other operators:

让我们回顾一下逻辑和位数OR运算符的优先级,以及其他运算符。

  • Operators with higher precedence: ++ –– * + – / >> << > < == !=
  • Bitwise AND: &
  • Bitwise OR: |
  • Logical AND: &&
  • Logical OR: ||
  • Operators with lower precedence: ? : = += -= *= /= >>= <<=

A quick example will help us understand this better:

一个简单的例子将帮助我们更好地理解这一点。

boolean result = 2 + 4 == 5 || 3 < 5;
assertTrue(result);

Considering the low precedence of the logical OR operator, the expression above will evaluate to:

考虑到逻辑OR运算符的低优先级,上面的表达式将评估为。

  • ((2+4) == 5) || (3 < 5)
  • And then, (6 == 5) || (3 < 5)
  • And then, false || true

This makes the result true.

这使结果成为真。

Now, consider another example with a bitwise OR operator:

现在,考虑另一个有位数OR运算符的例子。

int result = 1 + 2 | 5 - 1;
assertEquals(7, result);

The expression above will evaluate to:

上面的表达式将评估为。

  • (1+2) | (5-1)
  • And then, 3 | 4

Hence, the result will be 7.

因此,结果将是7

7. Conclusion

7.结语

In this article, we learned about using logical and bitwise OR operators on boolean and integer operands.

在这篇文章中,我们学习了在布尔和整数操作数上使用逻辑和位数OR运算符。

We also looked at the major difference between the two operators and their precedence among other operators.

我们还研究了这两个运算符之间的主要区别以及它们在其他运算符中的优先级。

As always, the example code is available over on GitHub.

像往常一样,示例代码可在GitHub上获得