Java Bitwise Operators

Java Bitwise Operators are used to perform bitwise operations on integer or char operands. Bitwise operations are done at bit level, meaning, operations like AND, OR, XOR, etc., are done between respective bits of the operands.

In this tutorial, we will learn about different Bitwise Operators available in Java programming language and go through each of these Bitwise Operations in detail, with the help of examples.

Operator Symbol – Example – Description

The following table specifies symbol, example, and description for each of the Assignment Operator in Java.

Bitwise
Operation
Operator
Symbol
ExampleDescription
AND&x & yReturns the bitwise AND operation between x and y.
OR|x | yReturns the bitwise OR operation between x and y.
XOR^x ^ yReturns the bitwise XOR operation between x and y.
Complement~~xReturns the complement of x.
Left Shift<<x << yReturns the result of x left shifted by y number of places.
Right Shift>>x >> yReturns the result of x right shifted by y number of places.

Bitwise AND Operator

In the following example, we take two integer values in x and y, and find the bitwise AND operation between x and y.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 9;
        //Bitwise AND
        int result = x & y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 1

Bitwise OR Operator

In the following example, we take two integer values in x and y, and find the bitwise OR operation between x and y.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 9;
        //Bitwise OR
        int result = x | y;
        System.out.println("Result : " + result);
    }
}

Output

</>
Copy
Result : 13

Bitwise XOR Operator

In the following example, we take two integer values in x and y, and find the bitwise XOR operation between x and y.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 9;
        //Bitwise XOR
        int result = x ^ y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 12

Bitwise Complement Operator

In the following example, we take an integer value in x, and find the bitwise complement of x.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 5;
        //Bitwise Complement
        int result = ~x;
        System.out.println("Result : " + result);
    }
}

Output

Result : -6

Bitwise Left Shift Operator

In the following example, we take two integer values in x and y, and find the left shift of x by y number of bits.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 5;
        int y = 3;
        //Bitwise Left-shift
        int result = x << y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 40

Bitwise Right Shift Operator

In the following example, we take two integer values in x and y, and find the right shift of x by y number of bits.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        int x = 49;
        int y = 3;
        //Bitwise Right-shift
        int result = x >> y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 6

Conclusion

In this Java Tutorial, we learned what Bitwise Operators are, and how to use them in Java programs, with the help of examples.