11.Write a program to in C check if a number is even or odd using the bitwise & AND operator.
Bitwise AND Operator (&):
The bitwise AND operator is a binary operator that logically ANDs each bit of two integer operands. Each bit of the operands is compared and if both are 1, 1 is returned. Otherwise, 0 is returned. This bitwise operator in C language is denoted by a single character/ampersand character, i.e. (&).
The syntax of the bitwise & AND operator is as follows:
operand1 and operand2
here,
Operand1 and operand2 refer to two integer inputs/variables on which low-level bitwise operations are to be performed. Note that operands can be any integer type, including int, short,char,long and byte.
The ampersand symbol & represents the bitwise operator.
In C Language the bitwise AND operator is represented by the symbol &.
Let us study the bitwise AND & operation on the integer number 5 and 3 .
a=5 0101
b=3 0011
Bitwise AND & Operation on 5 and 3
& a=5 --> 0101
b=3 --> 0011
ans= 0001
________
0001 = 1 (In decimal)
Idea to check even or odd number using & operator.
Check whether the last bit of the given number is set or not. If last bit of a number is set then the number is odd, otherwise it is even number.
As we know that bitwise AND (&) Operation of the Number by 1 is always 1, If it is odd because the last bit will be already set. Otherwise, it will results zero 0 as output.