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:

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 *