simple way to determine weather the given number is odd or even using & operator

& operator is very useful to determine whether the given number is odd or even in any programming language.it is called bitwise operator which operate using the bit of the number. i.e

1=001

2=010

3=011

it is in 3 bit representation.so our concern is whenever we do anding with & operator we get 0 or 1 depending upon nature of the number.if it is even we get 0 otherwise 1.

eg

var a=5;

if(a & 1){

//(“it produces 1”)

console.log(“this statement is true”)

}

now

a=4;

if(a &1){

// it produces zero

so if condition if false

}

Leave a comment