C Program#160 – To check whether a number is even or odd using bit-wise operator.

Program to check whether a number is even or odd using bit-wise operator.:

Program Logic :

  • Even number’s Least Significant Bit (LSB) is always OFF(0).such as – 2 (0010), 4 ( 0100), 6(0110), 8(1000), 98 (0110 0010)
  • Thus, Bitwise AND operation by 1 with EVEN, always results in Zero (0) using   number & 1  .
  • Odd number’s Least Significant Bit (LSB) is always ON(1).such as – 3 (0011), 5 ( 0101), 7(0111), 9(1001), 99 (0110 0011)
  • Thus, Bitwise AND operation by 1 with ODD, always results in ONE(1) using   number & 1  .
  • So it concludes to – if (   (number & 1)==1  ) its ODD, otherwise EVEN.
  • Still not understood ? See the proper working example in action below.




Full Well Commented, Self-Explanatory Example Program of the same given below.

Below Here are Two Output of the program.



Output of the Program (Run 1 ) : Input : ODD number.:

Output of the Program (Run 2 ) : Input : EVEN number.: