- Home
- Objectives

- XyzWs Study Guides
- Study Guides
- Study Notes
- Resources

- Mock Exams
SCJP Study Guide:
Fundamentals
Printer-friendly version |
Mail this to a friend
Bitwise Operators
The bitwise operators include the AND operator &, the inclusive OR operator |, the exclusive OR operator ^. The bitwise operators applies to boolean and integer types. Both operands are always evaluated.
The & operator computes the bitwise logical AND of the two operands, the | operator computes the bitwise logical OR of the two operands, and the ^ operator computes the bitwise logical exclusive OR of the two operands. No overflows are possible from these operations.
These operators have different precedence, with & having the highest precedence and | the lowest precedence. Each of these operators is syntactically left-associative (each groups left-to-right). Each operator is commutative if the operand expressions have no side effects. Each operator is associative.
Integer Bitwise Operators &, ^, and |
When both operands of an operator &, ^, or | are of a type that is convertible to a primitive integral type, binary numeric promotion is first performed on the operands. The type of the bitwise operator expression is the promoted type of the operands.
- For &, the result value is the bitwise AND of the operand values.
- For ^, the result value is the bitwise exclusive OR of the operand values.
- For |, the result value is the bitwise inclusive OR of the operand values.
The & operator performs a bitwise AND on two integers. Each bit in the result is 1 only if both corresponding bits in the two input operands are 1.
op1 op2 result 0 0 0 0 1 0 1 0 0 1 1 1
For example, 0x56 & 0x32 is 0x12, because (in binary):
0 1 0 1 0 1 1 0 & 0 0 1 1 0 0 1 0 --------------- 0 0 0 1 0 0 1 0
The | (vertical bar) operator performs a bitwise OR on two integers. Each bit in the result is 1 if either of the corresponding bits in the two input operands is 1.
op1 op2 result 0 0 0 0 1 1 1 0 1 1 1 1
For example, 0x56 | 0x32 is 0x76, because:
0 1 0 1 0 1 1 0 | 0 0 1 1 0 0 1 0 --------------- 0 1 1 1 0 1 1 0
The ^ (caret) operator performs a bitwise exclusive-OR on two integers. Each bit in the result is 1 if one, but not both, of the corresponding bits in the two input operands is 1.
op1 op2 result 0 0 0 0 1 1 1 0 1 1 1 0
For example, 0x56 ^ 0x32 is 0x64:
0 1 0 1 0 1 1 0 ^ 0 0 1 1 0 0 1 0 --------------- 0 1 1 0 0 1 0 0
For example, the following code uses xor to exchange two values (x and y) without using a third temporary variable. Never use it; this is just a curiosity from the museum of bizarre code.
class Program {
public static void main(String[] args) {
int x = 3;
int y = 5;
x = x ^ y;
y = x ^ y;
x = x ^ y;
System.out.println("X = " + x + ", Y = " + y);
}
}
Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or Boolean, then the type of the bitwise operator expression is boolean. In all cases, the operands are subject to unboxing conversion as necessary.
- The result of x & y is true if both x and y are true. Otherwise, the result is false.
- The result of x | y is true if either x or y is true. Otherwise, the result is false.
- The result of x ^ y is true if x is true and y is false, or x is false and y is true. Otherwise, the result is false. When the operands are of type boolean or Boolean, the ^ operator computes the same result as the != operator.
Bitwise Complement Operator ~
The ~ (tilde) operator performs a bitwise complement on its single integer operand. (The ~ operator is therefore a unary operator, like ! and the unary -, &, and * operators.) Complementing a number means to change all the 0 bits to 1 and all the 1s to 0s. For example, assuming 16-bit integers, ~0x56 is 0xffa9:
~ 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 ------------------------------- 1 1 1 1 1 1 1 1 1 0 1 0 1 0 0 1
The type of the operand expression of the unary ~ operator must be a type that is convertible to a primitive integral type, or a compile-time error occurs. Unary numeric promotion is performed on the operand. The type of the unary bitwise complement expression is the promoted type of the operand.
At run time, the value of the unary bitwise complement expression is the bitwise complement of the promoted value of the operand; note that, in all cases, ~x equals (-x)-1.
class Prorgam {
public static void main(String[] args) {
byte b = 2;
byte b1 = ~b; //compile-time error.
int i = ~b; //ok bitwise complement promotion
byte b2 = (byte)~b;
}
}
The type of the unary bitwise complement expression is the promoted type of the operand.