Java Bitwise AND

Java Bitwise AND Operator is used to perform AND operation between the respective bits of given operands.

Syntax

The syntax for Bitwise AND operation between x and y operands is

x & y

The operands can be of type int or char. Bitwise AND operator returns a value of type same as that of the given operands.

The following table illustrates the output of AND operation between two bits.

bit1 bit2 bit1 & bit2
0 0 0
0 1 0
1 0 0
1 1 1

Examples

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

Main.java

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

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

Main.java

public class Main {
    public static void main(String[] args) {
        char x = 'A';
        char y = 'B';
        //Bitwise AND
        int result = x & y;
        System.out.println("Result : " + result);
    }
}

Output

Result : 64

Conclusion

In this Java Tutorial, we learned what Bitwise AND Operator is, its syntax, and how to use this operator in Java programs, with the help of examples.