Java Program to Check Whether a Number is Even or Odd ( bit manipulation)

preview_player
Показать описание
The idea is to check whether the last bit of the number is set or not. If last bit is set then the number is odd, otherwise even.
As we know bitwise AND Operation of the Number by 1 will be 1, If it is odd because the last bit will be already set. Otherwise, it will give 0 as output

#BackCoding
Рекомендации по теме
Комментарии
Автор

import java.util.*;

class BTM
{
public static void OddOrEven(int n){
int bitMask=1;

if((n & bitMask)==0){
System.out.println("Is Even Number ");
}else{
System.out.println("Is Odd number ");
}
}
public static void main(String args[])
{
OddOrEven(4);
OddOrEven(17);

}
}

BackCoding
welcome to shbcf.ru