Bitwise Operators in Java

Bitwise operators in Java are used to perform operations on individual bits of an integer value. These operators work on the binary representation of the integer values and manipulate the bits according to the operator used. Some of the commonly used bitwise operators in Java are:


  1. 1. Bitwise AND ( & ): This operator performs a bitwise AND operation between two operands. The result is 1 if both the bits are 1, otherwise the result is 0.

  2. int a = 10; // binary representation: 0000 1010
    int b = 3;  // binary representation: 0000 0011
    int c = a & b; // binary representation: 0000 0010
    

    In this example, the result of the bitwise AND operation between a and b is 2.


  1. 2. Bitwise OR ( | ): This operator performs a bitwise OR operation between two operands. The result is 1 if either of the bits is 1, otherwise the result is 0.


  2. int a = 10; // binary representation: 0000 1010
    int b = 3;  // binary representation: 0000 0011
    int c = a | b; // binary representation: 0000 1011
    

    In this example, the result of the bitwise OR operation between a and b is 11.


  1. 3. Bitwise XOR ( ^ ): This operator performs a bitwise XOR operation between two operands. The result is 1 if the bits are different, otherwise the result is 0.


  2. int a = 10; // binary representation: 0000 1010
    int b = 3;  // binary representation: 0000 0011
    int c = a ^ b; // binary representation: 0000 1001
    

    In this example, the result of the bitwise XOR operation between a and b is 9.


  1. 4. Bitwise NOT ( ~ ): This operator performs a bitwise NOT operation on a single operand. It inverts the bits of the operand, i.e., 0 becomes 1 and 1 becomes 0.


  2. int a = 10; // binary representation: 0000 1010
    int b = ~a; // binary representation: 1111 0101
    

    In this example, the result of the bitwise NOT operation on a is -11.


  1. 5. Left Shift ( << ): This operator shifts the bits of the operand to the left by a specified number of positions. This results in multiplying the operand by 2 to the power of the number of positions shifted.


  2. int a = 10; // binary representation: 0000 1010
    int b = a << 1; // binary representation: 0001 0100
    

    In this example, the result of left shifting a by 1 position is 20.


  1. 6. Right Shift ( >> ): This operator shifts the bits of the operand to the right by a specified number of positions. This results in dividing the operand by 2 to the power of the number of positions shifted.


  2. int a = 10; // binary representation: 0000 1010
    int b = a >> 1; // binary representation: 0000 0101
    

    In this example, the result of right shifting a by 1 position is 5.


  1. 7. Unsigned Right Shift ( >>> ): This operator is similar to the right shift operator, but it does not preserve the sign of the operand.


  2. int a = -10; // binary representation: 1111 0110
    int b = a >>> 1; // binary representation: 0111 1011
    

    In this example, the result of the unsigned right shift operation on a is 1073741821.


These are just a few examples of how bitwise operators can be used in Java. The exact behavior of the bitwise operators depends on the type and size of the operands, so it's important to be aware of the underlying binary representation of the values.


These bitwise operators are useful in a variety of situations, such as setting and testing individual bits, checking if a number is odd or even, and other low-level operations.