Java Bitwise Operators

Introduction

Bitwise operators in Java are used to perform operations on individual bits of integer data types. These operators are particularly useful in low-level programming, such as in systems programming, encryption algorithms, and working with binary data. Java provides several bitwise operators that allow you to manipulate bits directly.

Key Points:

  • Operate on Bits: Bitwise operators work on the binary representation of numbers.
  • Applicable to Integer Types: These operators can be applied to data types like int, long, short, char, and byte.
  • Low-Level Operations: Commonly used in scenarios where direct bit manipulation is required.

List of Bitwise Operators

Java provides the following bitwise operators:

Operator Description Example Output
& Bitwise AND a & b Performs a bitwise AND operation between a and b
| Bitwise OR a | b Performs a bitwise OR operation between a and b
^ Bitwise XOR (exclusive OR) a ^ b Performs a bitwise XOR operation between a and b
~ Bitwise Complement ~a Inverts the bits of a
<< Left Shift a << 2 Shifts the bits of a to the left by 2 positions
>> Right Shift a >> 2 Shifts the bits of a to the right by 2 positions
>>> Unsigned Right Shift a >>> 2 Shifts the bits of a to the right by 2 positions, filling with zeros

Detailed Examples of Bitwise Operators

Let’s explore each bitwise operator with examples.

1. Bitwise AND (&)

The bitwise AND operator & compares each bit of its operands. If both bits are 1, the result is 1; otherwise, the result is 0.

public class BitwiseAndExample {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a & b;  // Binary: 0001

        System.out.println("a & b: " + result);  // Output: a & b: 1
    }
}

Explanation:

  • Bitwise Operation: The bitwise AND operation compares each bit of a and b, resulting in 0001, which is 1 in decimal.

Output:

a & b: 1

2. Bitwise OR (|)

The bitwise OR operator | compares each bit of its operands. If either bit is 1, the result is 1; otherwise, the result is 0.

public class BitwiseOrExample {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a | b;  // Binary: 0111

        System.out.println("a | b: " + result);  // Output: a | b: 7
    }
}

Explanation:

  • Bitwise Operation: The bitwise OR operation compares each bit of a and b, resulting in 0111, which is 7 in decimal.

Output:

a | b: 7

3. Bitwise XOR (^)

The bitwise XOR (exclusive OR) operator ^ compares each bit of its operands. If the bits are different, the result is 1; if they are the same, the result is 0.

public class BitwiseXorExample {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int b = 3;  // Binary: 0011
        int result = a ^ b;  // Binary: 0110

        System.out.println("a ^ b: " + result);  // Output: a ^ b: 6
    }
}

Explanation:

  • Bitwise Operation: The bitwise XOR operation compares each bit of a and b, resulting in 0110, which is 6 in decimal.

Output:

a ^ b: 6

4. Bitwise Complement (~)

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

public class BitwiseComplementExample {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int result = ~a;  // Binary: 1010 (in 32-bit signed integer representation)

        System.out.println("~a: " + result);  // Output: ~a: -6
    }
}

Explanation:

  • Bitwise Operation: The bitwise complement operation inverts the bits of a. In a 32-bit signed integer representation, ~0101 becomes 1010, which is -6 in decimal due to two’s complement representation.

Output:

~a: -6

5. Left Shift (<<)

The left shift operator << shifts the bits of its operand to the left by the specified number of positions. Zeros are added to the right.

public class LeftShiftExample {
    public static void main(String[] args) {
        int a = 5;  // Binary: 0101
        int result = a << 2;  // Binary: 10100

        System.out.println("a << 2: " + result);  // Output: a << 2: 20
    }
}

Explanation:

  • Bitwise Operation: The left shift operation shifts the bits of a (0101) two positions to the left, resulting in 10100, which is 20 in decimal.

Output:

a << 2: 20

6. Right Shift (>>)

The right shift operator >> shifts the bits of its operand to the right by the specified number of positions. The leftmost bits are filled with the sign bit (0 for positive numbers, 1 for negative numbers).

public class RightShiftExample {
    public static void main(String[] args) {
        int a = 20;  // Binary: 10100
        int result = a >> 2;  // Binary: 0101

        System.out.println("a >> 2: " + result);  // Output: a >> 2: 5
    }
}

Explanation:

  • Bitwise Operation: The right shift operation shifts the bits of a (10100) two positions to the right, resulting in 0101, which is 5 in decimal.

Output:

a >> 2: 5

7. Unsigned Right Shift (>>>)

The unsigned right shift operator >>> shifts the bits of its operand to the right by the specified number of positions. Zeros are added to the left, regardless of the sign of the number.

public class UnsignedRightShiftExample {
    public static void main(String[] args) {
        int a = -20;  // Binary (32-bit signed): 11111111111111111111111111101100
        int result = a >>> 2;  // Binary: 00111111111111111111111111111011

        System.out.println("a >>> 2: " + result);  // Output: a >>> 2: 1073741819
    }
}

Explanation:

  • Bitwise Operation: The unsigned right shift operation shifts the bits of a (11111111111111111111111111101100) two positions to the right, resulting in 00111111111111111111111111111011, which is 1073741819 in decimal.

Output:

a >>> 2: 1073741819

Conclusion

Java’s bitwise operators allow for direct manipulation of bits, making them used for low-level programming tasks. These operators can be used to optimize performance, perform bit-level calculations, and work with binary data.

Summary:

  • Bitwise AND (&): Compares each bit of two operands and returns 1 if both bits are 1.
  • Bitwise OR (|): Compares each bit of two operands and returns 1 if either bit is 1.
  • Bitwise XOR (^): Compares each bit of two operands and returns 1 if the bits are different.
  • Bitwise Complement (~): Inverts the bits of the operand.
  • Left Shift (<<): Shifts the bits to the left, filling the rightmost bits with zeros.
  • Right Shift (>>): Shifts the bits to the right, filling the leftmost bits with the sign bit (0 for positive numbers, 1 for negative numbers).
  • Unsigned Right Shift (>>>): Shifts the bits to the right, filling the leftmost bits with zeros, regardless of the sign of the number.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top