How do I do bitwise AND operation?

package org.kodejava.example.lang;

public class ANDDemo {
    public static void main(String[] args) {
        int numberA = 16;
        int numberB = 16;

        // Operator "&"  is used for doing bitwise AND operation
        int result = numberA & numberB;

        System.out.println(numberA + " & " + numberB + " = " + result);

        // Print the result in binary format
        System.out.println(Integer.toBinaryString(numberA) +
                " & " + Integer.toBinaryString(numberB) +
                " = " + Integer.toBinaryString(result));
    }
}

The result of the code snippet:

16 & 16 = 16
10000 & 10000 = 10000

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA. Support me by donating >> here <<.

Leave a Reply