Bitoperators

Bitwise operators in Java allow you to manipulate individual bits of integer data types (such as int, long, short, byte, and char). These operators are useful for tasks that require low-level data manipulation, such as cryptography, network programming, and performance optimization in algorithms.

Types of Bitwise Operators

1. Bitwise AND (&)

Performs a bitwise AND operation. Each bit in the result is set to 1 only if both corresponding bits of the operands are 1.

int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011
int result = a & b;  // Binary: 0001, Decimal: 1

2. Bitwise OR (|)

Performs a bitwise OR operation. Each bit in the result is set to 1 if at least one corresponding bit of the operands is 1.

int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011
int result = a | b;  // Binary: 0111, Decimal: 7

3. Bitwise XOR (^)

Performs a bitwise exclusive OR operation. Each bit in the result is set to 1 if the corresponding bits of the operands are different.

int a = 5;  // Binary: 0101
int b = 3;  // Binary: 0011
int result = a ^ b;  // Binary: 0110, Decimal: 6

4. Bitwise Complement (~)

Inverts all the bits of the operand (flips 0 to 1 and 1 to 0).

int a = 5;  // Binary: 0101
int result = ~a;  // Binary: 1010 (in 2's complement: -6), Decimal: -6

5. Left Shift (<<)

Shifts the bits of the operand to the left by the specified number of positions. Zero bits are shifted in from the right.

int a = 5;  // Binary: 0101
int result = a << 1;  // Binary: 1010, Decimal: 10

6. Right Shift (>>)

Shifts the bits of the operand to the right by the specified number of positions. For positive numbers, zero bits are shifted in from the left. For negative numbers, the sign bit is shifted in.

int a = 5;  // Binary: 0101
int result = a >> 1;  // Binary: 0010, Decimal: 2

7. Unsigned Right Shift (>>>)

Shifts the bits of the operand to the right by the specified number of positions, and zero bits are shifted in from the left regardless of the sign of the number.

int a = -5;  // Binary: 11111111111111111111111111111011
int result = a >>> 1;  // Binary: 01111111111111111111111111111101, Decimal: 2147483645

Code Example

Here is a complete Java program that demonstrates the use of bitwise operators:

public class BitwiseOperatorsDemo {

    public static void main(String[] args) {
        int x = 5;  // Binary: 0101
        int y = 3;  // Binary: 0011

        // Bitwise AND
        int andResult = x & y;
        System.out.println("x & y: " + andResult);  // Output: 1

        // Bitwise OR
        int orResult = x | y;
        System.out.println("x | y: " + orResult);  // Output: 7

        // Bitwise XOR
        int xorResult = x ^ y;
        System.out.println("x ^ y: " + xorResult);  // Output: 6

        // Bitwise Complement
        int complementResult = ~x;
        System.out.println("~x: " + complementResult);  // Output: -6

        // Left Shift
        int leftShiftResult = x << 1;
        System.out.println("x << 1: " + leftShiftResult);  // Output: 10

        // Right Shift
        int rightShiftResult = x >> 1;
        System.out.println("x >> 1: " + rightShiftResult);  // Output: 2

        // Unsigned Right Shift
        int unsignedRightShiftResult = x >>> 1;
        System.out.println("x >>> 1: " + unsignedRightShiftResult);  // Output: 2

        // Unsigned Right Shift with a negative number
        int negativeNumber = -5;
        int unsignedRightShiftNegativeResult = negativeNumber >>> 1;
        System.out.println("negativeNumber >>> 1: " + unsignedRightShiftNegativeResult);  // Output: 2147483645
    }
}