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.

Frequently Asked Questions

How do bitwise AND (&) and OR (|) operators work in Java?

The bitwise AND (&) and OR (|) operators perform bit-level operations on integers, comparing or combining corresponding bits from two values.

What is the purpose of the bitwise XOR (^) operator in Java?

The bitwise XOR (^) operator returns 1 for bits that are different between two values and 0 for bits that are the same, making it useful for toggling specific bits.

What do the left shift (<<) and right shift (>>) operators do in Java?

The left shift operator shifts the bits of a number to the left by the specified number of positions. This is equivalent to multiplying the number by 2 raised to the power of the shift amount.
The right shift operator shifts the bits of a number to the right by the specified number of positions. This is equivalent to dividing the number by 2 raised to the power of the shift amount.

What is the unsigned right shift (>>>) operator used for in Java?

The unsigned right shift (>>>) operator shifts bits to the right, filling the leftmost positions with zeros. It is used when you want to treat the value as an unsigned number.

Are bitwise and shift operators commonly used in Java programming?

Bitwise and shift operators are used in specific scenarios like low-level system programming, cryptography, and certain optimization tasks, but they are less common in typical Java applications.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments