Java bitwise AND to check a given number is odd or even

In this post let us learn java bitwise AND to check a given number is odd or even.

Java bitwise AND to check a given number is odd or even

Basically even number is divisible by 2 and it’s remainder will be 0. Meanwhile for odd number remainder will be 1 when divided by 2.

Also read – Even odd program in java using ternary operator

Bitwise AND (&) operator returns bit by bit AND of input values, that is, if both bits are 1, it gives 1, else it gives 0. Here we have check if last bit is 1. If last bit is 1 then it is odd else it is even.

Let’s see java program to check whether number is even or odd using bitwise AND (&),

public class EvenOddBitwise
{
static int checkEven(int num)
{
return(num & 1);
}
   public static void main(String[] args) 
{
int number = 501;
if(checkEven(number) == 0)
{
System.out.print("Even number");
}
else
{
System.out.print("Odd number");
}
}
}

Output:

Odd number