Let’s 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

In this post we are going to learn to find number even odd using bitwise operator in java. Basically to check if number is even we divide it by 2 and its remainder will be 0.

Meanwhile to check if number is odd its remainder will be 1 when divided by 2.

Bitwise AND (&) operator returns bit by bit of input values in binary representation. Here we have to check right most significant bit.

If the right most significant bit is 1 then it is odd number else it is even number. Also integer numbers are represented as 2’s complement. For even number it has 0 as there Least Significant Bit (LSB).

Here’s program to check a given number is even or odd using bitwise (&) operator.

import java.util.Scanner; public class EvenOddBitwise { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter a number: "); int number = sc.nextInt(); if((number & 1) == 0) { System.out.println(number + " is an even number."); } else { System.out.println(number + " is an odd number."); } sc.close(); } }

**Output:**

Please enter a number: 56

56 is an even number.

Meanwhile we can also check a given number is even or odd using division operator( / ).

Here division operator basically returns quotient. For example, if we take a number and divide by two and multiply result with 2 then the number will be less in case of odd number and equal in case of even number.

int result = number / 2; if(result * 2 == number) { System.out.println("Even number"); }

**Check if a number is even or odd using bitwise xor operator in java**

Similarly we can check if a number is even or odd using bitwise xor operator.

public class UsingBitwiseOperator { static boolean checkEven(int number) { if((number ^ 1) == number + 1) return true; else return false; } public static void main(String[] args) { int num = 54; System.out.println(checkEven(num) == true ? "Even" : "Odd"); } }

**Output:**

Even

public class UsingBitwiseOperator { static boolean checkEven(int number) { if((number ^ 1) == number + 1) return true; else return false; } public static void main(String[] args) { int num = 51; System.out.println(checkEven(num) == true ? "Even" : "Odd"); } }

**Output:**

Odd

Also read – abstraction in java