Bitwise & Shift Operators in Java

A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left. The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1.

| Bitwise OR
& Bitwise AND
^ Bitwise XOR
~ Bitwise complement
<< Left shift
>> Signed right shift
>>> Unsigned right shift

This Java program demonstrates the usage of bitwise operators (&, |, ^, ~) in Java. In this program, we first declare two integer variables a and b and assign them the values of 25 and 45 respectively. We then use the bitwise AND operator (&) to perform bitwise AND operation on a and b, and print the result using System.out.println() method.

We then use the bitwise OR operator (|) to perform bitwise OR operation on a and b, and print the result. Next, we use the bitwise XOR operator (^) to perform bitwise XOR operation on a and b, and print the result.

Finally, we use the bitwise NOT operator (~) to perform bitwise complement operation on a and print the result. The bitwise NOT operator returns the complement of the operand, which means all the bits in the operand are inverted. In this case, since a is 25 (binary 00011001), its complement is -26 (binary 11100110 in two's complement representation).

Source Code

public class Bitwise {
    public static void main(String args[])
        //Bitwise & Shift Operators in Java
        int a=25,b=45;
        System.out.println("Bitwise And : "+(a&b));
        System.out.println("Bitwise Or : "+(a|b));
        System.out.println("Bitwise Xor : "+(a^b));
        System.out.println("Bitwise Not : "+(~a));


Bitwise And : 9
Bitwise Or : 61
Bitwise Xor : 52
Bitwise Not : -26
To download raw file Click Here

Basic Programs