DEV Community

Amit Mishra
Amit Mishra

Posted on

Find odd even using Bitwise operator.

Here we'll use & operator to find thae number is Odd or Even.

public class JavaOneWorld {
public static void main(String 
     args[]) {
     int x= 7;

Enter fullscreen mode Exit fullscreen mode

The idea behind using the & operator !!!!!

As we already know binary works only on 1 and 0.
and a number is either odd or even,
so if a number is even then the last bit of that number must be 0 and if odd then the last bit must be 1.


5 odd
101 last bit is 1.

4 even

100 last bit is 0.

so here we have to use this property to find out the odd/even

Deep dive in this post...

Discussion (0)