Bitwise and Shift Operator in Java

Bitwise and shift operator in Java are used to perform low-level bit manipulation on integers. These operators work on individual bits of integer values and are often used in scenarios where we need to perform operations at the binary level for example low-level hardware.

Bitwise AND operator (&)

The bitwise AND operator compares each bit of two integer operands and produces a new integer with a 1-bit only if both corresponding bits in the operands are 1.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a & b;  // Binary: 0001 (Decimal: 1)
        System.out.println(result);
    }
}

Output:

1

In the above example, a & b results in 1 because only the last bit is 1 in both a and b.

Bitwise OR operator (|)

The bitwise OR operator compares each bit of two integer operands and produces a new integer with a 1-bit if either of the corresponding bits in the operands is 1.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a | b;  // Binary: 0111 (Decimal: 7)
        System.out.println(result);
    }
}

Output:

7

In the above example, a | b results in 7 because the binary representation of a and b have their corresponding bits set to 1 at some positions, and these 1-bits are combined in the result.

Bitwise XOR operator (^)

The bitwise XOR (exclusive OR) operator compares each bit of two integer operands and produces a new integer with a 1-bit only if the corresponding bits in the operands differ.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a ^ b;  // Binary: 0110 (Decimal: 6)
        System.out.println(result);
    }
}

Output:

6

In the above example, a ^ b results in 6 because the binary representation of a and b have their corresponding bits different at some positions, and these differing bits are combined in the result.

Bitwise NOT operator (~)

The bitwise NOT operator performs a one’s complement operation, which flips each bit of the operand.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;   // Binary: 0101
        int result = ~a;  // Binary: 1010 (Decimal: -6)
        System.out.println(result);
    }
}

Output:

-6

In the above example, ~a results in -6. This happens because Java uses a two’s complement representation for negative numbers, and the bitwise NOT operation inverts all bits, including the sign bit, which results in a negative value.

Left shift operator (<<)

The left shift operator shifts the bits of the left-hand operand to the left by the number of positions specified on the right-hand side.

Example:

class Test {
    public static void main(String[] args) {
        int a = 5;       // Binary: 0101
        int shifted = a << 2;  // Binary: 010100 (Decimal: 20)
        System.out.println(shifted);
    }
}

Output:

20

In the above example, a << 2 shifts the bits of a two positions to the left, and the result is 20.

Right shift operator (>>)

The right shift operator shifts the bits of the left-hand operand to the right by the number of positions specified on the right-hand side. It performs arithmetic right shift, meaning the sign bit is replicated to preserve the sign of the number.

Example:

class Test {
    public static void main(String[] args) {
        int a = 40; // 101000 in binary
        int b = a >> 2; // 1010 in binary

        System.out.println(b); // 10
    }
}

Output:

10

In the above example, a >> 2 shifts the bits of a two positions to the right, and the result is -3.

Unsigned right shift operator (>>>)

The unsigned right shift operator shifts the bits of the left-hand operand to the right by the number of positions specified on the right-hand side. It performs logical right shift, and the leftmost positions are filled with zeros.

Example:

class Test {
    public static void main(String[] args) {
        int a = -10;       // Binary: 11111111111111111111111111110110
        int shifted = a >>> 2;  // Binary: 00111111111111111111111111111101 (Decimal: 1073741821)
        System.out.println(shifted);
    }
}

Output:

1073741821

In the above example, a >>> 2 shifts the bits of a two positions to the right, and the result is 1073741821.

Sharing Is Caring:
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments