Find number even odd using bitwise operator in java

Hey guys!!! Welcome to flower brackets blog. In this post let us learn how to find number even odd using bitwise operator in java.

Find number even odd using bitwise operator in java

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 bitwise AND (&) to check a given number is odd or even.

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

Related Posts