|
We have earlier seen the bitwise complement operator ~. This unary operator inverts the bits in its operand. This behavior is identical to the NOT logical operation. You can perform other logical operations such as AND, OR and XOR on binary operands with the help of three java operators &, | and ^. However, you need to know two things to predict the result of these operators accurately:
1. The bit representation of operands
2. The truth tables of these three logical operations
|
|
Computer is a digital device where data is represented in terms on 1s and 0s. The digital computation logic is based on certain rules that decide the output when certain input bits are passed. These rules are categorized into logical operations (AND, OR, XOR, NOT and so on). Each of these logical operations is defined in terms of the truth table. This table specifies what an output should be for the given combinations of inputs. |
The &, | and ^ operators are applicable to integral numeric types and the boolean data type. They operate on bit representation. Table 9.5 represents the truth tables for all three operations. The inputs and the outputs are represented for a single bit value.
Table 9.5 Truth table of AND, OR and XOR operations
|
First Input Bit bit1 |
Second Input Bit bit2 |
Output Bit |
||
|
bit1 & bit2 AND |
bit1 | bit2 OR |
bit1 ^ bit2 (XOR) |
||
|
0 |
0 |
0 |
0 |
0 |
|
0 |
1 |
0 |
1 |
1 |
|
1 |
0 |
0 |
1 |
1 |
|
1 |
1 |
1 |
1 |
0 |
When these operators are applied to binary operands, first the bit representations for both the operands are taken. Then the operator is applied to the bit-representations on bit-by-bit basis to produce the result. In case of boolean operands, they operate directly on the boolean values.
The & operator performs logical AND operation on the bit representation of operands. In AND operation, the output is 1 only when both the inputs, the first and the second input, are 1. For any other combinations of input bits, the output is 0. You can perform the bitwise AND on Java operands as:
byte a = 6; // Bit representation 00000110
byte b = 5; // Bit representation 00000101
byte c = a & b; // Bit representation 00000100
As you can see, you are applying the truth table of AND to every bit of the first operand (byte value 6) to the corresponding bit of second operand (byte value 5) to form the result. As only the 3rd bit from left of both the operands are 1, the same 3rd bit in the result is 1. Rest of the bits are 0.
![]() |
Figure 9.6 Usage of bitwise & operator on int operands
|
|
You need not worry about finding the bit representation for operands. It may be given in the question itself. Sometimes, the operands are presented as hexadecimal value, so that you can find the bit presentation easily. For that you need to know the bit representation of 0 (0000) to F (1111) hexadecimal numbers. However, you must know the truth tables very well to predict the result of operations. |
The | operator performs logical OR operation on the bit representation of its operands. In OR operation, the output is 1 when any of the inputs, either the first or second input bit is 1. The output bit is 0 when both input bits are 0. Figure 9.7 illustrates the working of | operator on int operands 6 and 5.
![]() |
Figure 9.7 Usage of bitwise | operator on int operands
The ^ operator performs logical XOR operation on the bit representation of operands. In XOR (eXclusive-OR) operation, the output bit is 1 if either, but not both, of the input bits is 1. The output bit is 0 if both inputs bits are 0 and also when both inputs are 1. Another way of looking at XOR operator is that the output is 1 if the inputs are different (exclusive of each other), but 0 if the inputs are the same. Figure 9.8 illustrates the working of ^ operator on int operands 6 and 5.
![]() |
Logical operators & | and ^ for boolean operands
The & | and ^ operators are also applicable to the boolean operands. They work almost same on boolean operands as they work on the integral operands. The inputs are however considered as true and false instead of 1 and 0. The truth table for integral operands is applicable with boolean operands with a slight modification. Table 9.6 show the output of logical AND, OR and XOR operations for boolean inputs.
Table 9.6 Truth table of AND, OR and XOR operations for boolean operands
|
Input Operand1 |
Input Operand2 |
Output |
||
|
op1 & op2 AND |
op1 | op2 OR |
op1 ^ op2 XOR |
||
|
false |
false |
false |
false |
false |
|
false |
true |
false |
true |
true |
|
true |
false |
false |
true |
true |
|
true |
true |
true |
true |
false |
You can apply this truth table to boolean operands to get the output. The output is always of boolean type. Therefore, these operators are often used to specify conditions. Following are few examples of using the boolean logical operators:
![]() |
Java has two more operators performing the logical AND and OR operations called as the short circuit operators. The next section discusses how they are different from & and |.