Java Bitwise Operators


Bitwise operators in Java are used to perform operations on the individual bits of integer types (such as int, long, short, and byte). These operators allow you to manipulate bits directly, which can be useful for tasks such as low-level programming, graphics programming, and certain algorithms. Here’s a detailed explanation of bitwise operators along with examples.

List of Bitwise Operators in Java

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XOR (exclusive OR)a ^ b
~Bitwise NOT (one's complement)~a
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

1. Bitwise AND Operator (&)

The bitwise AND operator compares each bit of two operands. It returns 1 if both bits are 1; otherwise, it returns 0.

Example:

public class BitwiseAndExample { public static void main(String[] args) { int a = 5; // In binary: 0101 int b = 3; // In binary: 0011 int result = a & b; // Result: 0101 & 0011 = 0001 (1 in decimal) System.out.println("Bitwise AND: " + result); // Output: Bitwise AND: 1 } }

2. Bitwise OR Operator (|)

The bitwise OR operator compares each bit of two operands. It returns 1 if at least one of the bits is 1.

Example:

public class BitwiseOrExample { public static void main(String[] args) { int a = 5; // In binary: 0101 int b = 3; // In binary: 0011 int result = a | b; // Result: 0101 | 0011 = 0111 (7 in decimal) System.out.println("Bitwise OR: " + result); // Output: Bitwise OR: 7 } }

3. Bitwise XOR Operator (^)

The bitwise XOR operator compares each bit of two operands. It returns 1 if the bits are different (i.e., one is 1 and the other is 0).

Example:

public class BitwiseXorExample { public static void main(String[] args) { int a = 5; // In binary: 0101 int b = 3; // In binary: 0011 int result = a ^ b; // Result: 0101 ^ 0011 = 0110 (6 in decimal) System.out.println("Bitwise XOR: " + result); // Output: Bitwise XOR: 6 } }

4. Bitwise NOT Operator (~)

The bitwise NOT operator inverts the bits of its operand. Each 0 becomes 1, and each 1 becomes 0.

Example:

public class BitwiseNotExample { public static void main(String[] args) { int a = 5; // In binary: 0101 int result = ~a; // Result: 1010 (which is -6 in decimal due to two's complement representation) System.out.println("Bitwise NOT: " + result); // Output: Bitwise NOT: -6 } }

5. Left Shift Operator (<<)

The left shift operator shifts the bits of the operand to the left by a specified number of positions. New bits are filled with 0.

Example:

public class LeftShiftExample { public static void main(String[] args) { int a = 5; // In binary: 0101 int result = a << 2; // Result: 0101 << 2 = 10100 (20 in decimal) System.out.println("Left Shift: " + result); // Output: Left Shift: 20 } }

6. Right Shift Operator (>>)

The right shift operator shifts the bits of the operand to the right by a specified number of positions. The sign bit (leftmost bit) is used to fill the new bits.

Example:

public class RightShiftExample { public static void main(String[] args) { int a = 20; // In binary: 10100 int result = a >> 2; // Result: 10100 >> 2 = 00101 (5 in decimal) System.out.println("Right Shift: " + result); // Output: Right Shift: 5 } }

7. Unsigned Right Shift Operator (>>>)

The unsigned right shift operator shifts the bits to the right and fills the new bits with 0, regardless of the sign of the original number.

Example:

public class UnsignedRightShiftExample { public static void main(String[] args) { int a = -20; // In binary: 11111111 11111111 11111111 11101100 int result = a >>> 2; // Result: 00111111 11111111 11111111 11111011 (1073741821 in decimal) System.out.println("Unsigned Right Shift: " + result); // Output: Unsigned Right Shift: 1073741821 } }

Summary

Bitwise operators in Java provide a way to manipulate individual bits of integer data types. They are useful for performance optimization and tasks requiring direct manipulation of binary data. Understanding how to use these operators can help you write more efficient algorithms and perform low-level operations effectively.