Check The Programming Section
The bitwise AND operator (&) is different from logical AND operator (&&). Logical && compares multiple relationships between operands and bitwise & works on bits of the operands. The bitwise & requires two operands of type char or int. Consider the example listed below:
int k = a & b;
The operand a and b either variables or constnats of type int or char. The second operand b is often called an AND mask. The bitwise & operates bits of a’s and b’s and the resultant bit is decided by the resultant bit displayed in the Table below:
The example given below clears the complete idea while ANDing of one operand by another. The rules listed in the above Table will be applicable here.
From the above example, now it must be clear that operation on bits is performed pairwise. Operation performed on one pair does not affect the operation performed on another pair of bits.
#include<stdio.h>
void getBits(short int num){
int i = 15;
int bin[16] = {0};
while(num>0){
bin[i] = num%2;
i--;
num/=2;
}
for(int i=0;i<=15;i++){
printf("%d",bin[i]);
}
}
int main(){
short int num = 21;
short int mask = 8;
printf("Operand in Binary :");
getBits(num);
printf("\n");
printf("Mask in Binary :");
getBits(mask);
short int result = num & mask;
printf("\n\t\t ------------------\n");
printf("Result in Binary :");
getBits(result);
}
Operand in Binary :0000000000010101
Mask in Binary :0000000000001000
-----------------
Result in Binary :0000000000000000
The bitwise AND operator is mainly used to check, whether a bit in a given operand is ON or OFF. If a bit is 1, it means bit is ON and is 0 for OFF. The bitwise AND operator is mainly used to check, whether a bit in a given operand is ON or OFF. If a bit is 1, it means bit is ON and is 0 for OFF. Suppose 11001100 is the bit pattern of the given operand and we want to check if bit number 3 is ON (1) or OFF (0). Then the second operand the AND mask should be equal to given as below:
AND mask = 1 * 23 = 8
Then the ANDing operation would be
11001100 The Original bit pattern
00001000 AND Mask
--------
00001000 Resulting bit pattern
The resultant bit number 3 is ON (1), which means the bit number 3 in given operand is also ON (1). If the bit number 3 in the given operand is OFF (0), then the result would be 00000000.
Thus depending upon the bit number to be checked in the first operand, we can calculate the second operand which is called AND mask. If the bit is ON (1), the resulting value would turns out to be a non-zero value equal to the second operand. And if the bit is OFF (0), then the resultant value would be equal to zero.
#include<stdio.h>
#include<math.h>
void getBits(short int num){
int i = 15;
int bin[16] = {0};
while(num>0){
bin[i] = num%2;
i--;
num/=2;
}
for(int i=0;i<=15;i++){
printf("%d",bin[i]);
}
}
int main(){
int num = 56;
//check the 5th bit of num
int mask = 1 * pow(2, 5);
int res = num & mask;
if(res==0){
printf("The bit number 5 of %d is OFF\n",num);
getBits(num);
}
else
printf("The bit number 5 of %d is ON\n", num);
getBits(num);
}
The bit number 5 of 56 is ON
The equal importance of logical & operator is changing the status of a bit and most importantly to switch OFF a particular bit. Consider the given number as 10011001 and we want to switch OFF the bit number 4, so the required AND mask will be 11101111. Similarly to switch OFF the bit number 0 the required AND mask will be 1111110. The idea is, we simply design an AND mask, which will have all 1’s except that particular bit in AND mask must be set to zero (0).
Example 1
10011001
11101111 (set OFF for bit no. 5)
---------
10001001 (The resultant number)
Example 2
10011001
11111110 (set OFF for bit no. 0)
---------
10011000 (The resultant number)