About Lesson
These operators are used for long, int, short, char and byte. They affect & process the bits of the operands.
All integer types (except char) are signed integers. Java uses two’s complement to represent integer (Refer earlier notes).
Highest order bit denotes the sign of an integer (1 denotes negative, 0 is positive).
Bitwise Logical Operators (&
, |
, ^
and ~
)
~ NOT
operator: bitwise complement: inverts all bits of operand. eg suppose number 5 represented in one byte would be00000101
NOT of 5 would be11111010
public class BitwiseNotExample {
public static void main(String[] args) {
int a = 5; // Binary: 0000 0000 0000 0000 0000 0000 0000 0101
int result = ~a; // Bitwise NOT: Flips each bit to its complement
// Binary: 1111 1111 1111 1111 1111 1111 1111 1010 (Two's complement representation)
// Decimal: -6
System.out.println("Original Value: " + a);
System.out.println("Bitwise NOT Result: " + result);
}
}
& AND
operator: results in1
bit if both operands are 1, else0
eg00000101 & 11100110
would be00000100
public class BitwiseAndExample {
public static void main(String[] args) {
// Define two integers
int a = 13; // Binary: 0000 1101
int b = 6; // Binary: 0000 0110
// Bitwise AND operation
int result = a & b; // Result: 0000 0100 (Decimal: 4)
// Output the result
System.out.println("Bitwise AND Result: " + result);
}
}
| OR
operator: results in1
if bit of either of the operands is1
else0
if both bits are0
public class BitwiseOrExample {
public static void main(String[] args) {
// Define two integers
int a = 13; // Binary: 0000 1101
int b = 6; // Binary: 0000 0110
// Bitwise OR operation
int result = a | b; // Result: 0000 1111 (Decimal: 15)
// Output the result
System.out.println("Bitwise OR Result: " + result);
}
}
^ XOR
operator: if exactly one operand’s bit is1
, then result is1
, else0
.00101001 ^ 10110000
would be10011001
public class BitwiseXorExample {
public static void main(String[] args) {
// Define two integers
int a = 13; // Binary: 0000 1101
int b = 6; // Binary: 0000 0110
// Bitwise XOR operation
int result = a ^ b; // Result: 0000 1011 (Decimal: 11)
// Output the result
System.out.println("Bitwise XOR Result: " + result);
}
}
<<
Left Shift Operator : This operator shifts the bits of a binary number to the left by a specified number of positions. Each bit is moved to the left by the specified number of position and vacant positions on the right are filled with zeroes. Left shift operator is commonly used to multiply an integer by a power of 2. Each left shift byn
positions effectively multiplies the number by2^n
.
public class LeftShiftExample {
public static void main(String[] args) {
int num = 5; // Binary: 0000 0101
// Left shift by 2 positions
int result = num << 2; // Binary result: 0001 0100 (Decimal: 20)
// Output the result
System.out.println("Left Shift Result: " + result);
}
}
>>
Right Shift Operator: This operator shifts the bits of a binary number to the right by a specified number of positions. Each bit is moved to the right by the specified number of positions, and the vacant positions on the left are filled with the sign bit (0 for positive numbers, 1 for negative numbers). The right shift operator is commonly used for division by powers of 2. Each right shift byn
positions effectively divides the number by2^n
.
public class RightShiftExample {
public static void main(String[] args) {
int num = 20; // Binary: 0001 0100
// Right shift by 2 positions
int result = num >> 2; // Binary result: 0000 0101 (Decimal: 5)
// Output the result
System.out.println("Right Shift Result: " + result);
int negativeNum = -12; // Binary: 1111 1111 1111 1111 1111 1111 1111 0110
int negativeResult = negativeNum >> 2;
System.out.println("Right Shift Result: " + negativeResult);
}
}
>>>
Unsigned Right Shift Operator: This operator shifts the bits of a binary number to the right by a specified number of positions. Unlike the right shift operator (>>
), the unsigned right shift operator fills the vacant positions on the left with zeros, regardless of the sign bit.
public class UnsignedRightShiftExample {
public static void main(String[] args) {
int num = -20; // Binary: 11111111111111111111111111101100 (32-bit two's complement representation)
// Unsigned right shift by 2 positions
int result = num >>> 2; // Binary result: 00111111111111111111111111111011 (1073741819 in decimal)
// Output the result
System.out.println("Unsigned Right Shift Result: " + result);
}
}
Bitwise Operator Compound Assignments
It combines bitwise operation with assignment into a single statement. Compound assignments perform the specified bitwise operation between the left operand and the right operand, and then assign the result back to the left operand.
Types: &=
, |=
, ^=
, >>=
, >>>=
and <<=
public class BitwiseCompoundAssignmentExample {
public static void main(String[] args) {
int num1 = 5; // Binary: 0000 0101
int num2 = 3; // Binary: 0000 0011
// Bitwise AND compound assignment
num1 &= num2; // Equivalent to: num1 = num1 & num2
System.out.println("Bitwise AND Compound Assignment Result: " + num1); // Output: 1
// Bitwise OR compound assignment
num1 = 5; // Reset num1
num1 |= num2; // Equivalent to: num1 = num1 | num2
System.out.println("Bitwise OR Compound Assignment Result: " + num1); // Output: 7
// Bitwise XOR compound assignment
num1 = 5; // Reset num1
num1 ^= num2; // Equivalent to: num1 = num1 ^ num2
System.out.println("Bitwise XOR Compound Assignment Result: " + num1); // Output: 6
// Left shift compound assignment
num1 = 5; // Reset num1
num1 <<= 2; // Equivalent to: num1 = num1 << 2
System.out.println("Left Shift Compound Assignment Result: " + num1); // Output: 20
// Right shift compound assignment
num1 = 20; // Reset num1
num1 >>= 2; // Equivalent to: num1 = num1 >> 2
System.out.println("Right Shift Compound Assignment Result: " + num1); // Output: 5
// Unsigned right shift compound assignment
num1 = -20; // Reset num1
num1 >>>= 2; // Equivalent to: num1 = num1 >>> 2
System.out.println("Unsigned Right Shift Compound Assignment Result: " + num1); // Output: 1073741819
}
}