Java Program on Bitwise AND Operator

In the previous article we have discussed about Java Program on Bitwise Unsigned Right Shift Operator

In this article we are going to see the use Bitwise AND operator in Java.

Java Program on Bitwise AND Operator

Bitwise AND Operator:

Bitwise AND operator is a binary operator which is represented by & symbol. It performs manipulation on each bit of a number i.e. bit by bit AND operation. It means it returns only 1 if both the bits are 1 else it returns 0.

Java Bitwise AND Operator

Syntax: 

operand1 & operand2

Where,

  • operand1 refers to first operand
  • &¬†refers to Bitwise AND operator
  • operand2 refers to second operand

Example:

A = 2 in binary 0010
B = 3 in binary 0011
A & B = 2 = 0010

Let’s see an program to understand it more clearly.

Approach:

  • Declare two number.
  • Perform Bitwise AND operation on them by using operator.
  • Print the result.
public class Main
{   
    public static void main(String[] args)   
    {   
        //declaring two numbers
        int a = 2,  b = 3;   
        //performing Bitwise AND operation
        int result = a & b;
        //printing result
        System.out.println("a & b = "+result);   
    }  
}
Output:

a & b = 2

topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: