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

</>
Copy
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.

bit1bit2bit1 & bit2
000
010
100
111

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

</>
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

In the following example, we take char 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) {
        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.