1. Introduction
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.
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.
In this tutorial, we'll learn about the logical and bitwise OR operators, represented by || and | respectively.
2. Use of Logical OR
2.1. How It Works
The logical OR operator works on boolean operands. It returns true when at least one of the operands is true, otherwise, it returns false:
- true || true = true
- true || false = true
- false || true = true
- false || false = false
2.2. Example
Let's understand with the help of some boolean variables:
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:
boolean result = condition1 || condition2;
assertTrue(result);
When we apply logical OR on one true and one false operand, the result will be true:
boolean result = condition1 || condition3;
assertTrue(result);
And when we apply logical OR on two false operands, the result will be 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:
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.
3. Short-Circuit
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.
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:
boolean result1 = returnAndLog(true);
boolean result2 = returnAndLog(false);
if (result1 || result2) {
}
Output:
Returning true
Returning false
4. Use of Bitwise OR
4.1. How It Works
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:
- 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.
4.2. Example
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 0100
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.
When there are multiple operands, the evaluation is done from left to right. So, the expression 1 | 2 | 3 | 4 will be evaluated as:
int result1 = 1 | 2;
int result2 = result1 | 3;
int finalResult = result2 | 4;
assertEquals(finalResult,7);
5. Compatible Types
In this section, we'll look at the data types that these operators are compatible with.
5.1. Logical OR
The logical OR operator can only be used with boolean operands. And, using it with integer operands results in a compilation error:
boolean result = 1 || 2;
Compilation error: Operator '||' cannot be applied to 'int', 'int
5.2. Bitwise 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.
Let's understand with the help of some boolean variables in an example:
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
Let's review the precedence of logical and bitwise OR operator, among other operators:
- 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:
- ((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:
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. Conclusion
In this article, we learned about using logical and bitwise OR operators on boolean and integer operands.
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.
The post Logical vs Bitwise OR Operator first appeared on Baeldung.