BITWISE AND (&) OPERATOR
Bitwise operators work on binary digits. The bitwise AND operator performs logical AND operation on pair of bits. Consider two numbers 2 and 1, then 2 & 1 will result in 0.
2: 010
1: 001
--------
000
Explanation:
0 & 1 = 0
1 & 0 = 0
0 & 0 = 0
Now, we can apply this for determining whether a given number is even or odd. The bitwise AND operation of any even number with 1 will always result in 0.
// Program to check whether the number is even or odd using Bitwise AND (&) operator
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"ENTER THE NUMBER TO BE CHECKED:\n";
cin>>num;
if((num & 1)== 0 ) // Using Bitwise AND (&) operator
{
cout<<"NUMBER IS EVEN";
}
else
{
cout<<"NUMBER IS ODD";
}
return 0;
}