How would you check if a number is even or odd using bit wise operator in Java?

By | August 17, 2019

In this post we will talk and learn How would you check if a number is even or odd using bit wise operator in Java? using an example.

If you look carefully for all Odd numbers rightmost bit is always 1 in binary representation.

Notes
We should prefer Bitwise operator for checking even or odd because the traditional way of checking even by n % 2 ==0 is compassionately expensive compared to Bitwise & operator (Big O(1) time complexity)

Let’s try to understand above concept using couple of examples.

  1. Check a number is odd or even using modulus operator

 2. Check a number is odd or even without modulus(using bitwise) operator

Sample input/output of above programs:

You May Also Like:

How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
Find largest and second largest number in the given Array
Java program to Remove Duplicate Elements From an Array
Find common elements between two Arrays
Find largest and smallest number in the given Array
Java Program to find duplicate elements in an Array
Count number of words in a string in java
How can you check if the given number is power of 2?
How to check if String is number in java ?

That’s all about How would you check if a number is even or odd using bit wise operator in Java?
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *