Masking, Setting, Clearing and Testing of Bits

In the process of masking, we mask or filter bits of a variable. The variable is called the original value and the trail of bits that we are masking to the original value is called a mask. In this process, some bits of variables change and other bits remain as they are.

We can do masking using bitwise AND, OR, XOR operators. Masking means we can change bits of a variable to 1 or 0 and other bits remain as it is. 

Masking using bitwise AND operator (Clearing bits)

As the characteristics of a bitwise AND operator, if we AND 1 with any bit then it remains as it is. If we AND 0  with any bit then it becomes 0 which is called Clearing of bits.

Let us understand this by the below example.

Ex. a = 10; // original value

And suppose the mask is 00000011. So I want the result of a&00000011 so convert it to a binary. The binary representation of int is 8 bits.

a(binary) = 00001010

Now each individual bit of original value and mask will be added to each other individually.

a&00000011 = ( 0000 1010 )  & (  0000 0011 )  = ( 0000 0010 ) (decimal : 2 )

So the result of a&00000011 is 2. The same thing is done in the below program. Where b is the mask. And the result is printed.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=10,b=3;
  clrscr();
  printf("result : %d ",a & b);
  getch();
}

Output:

Masking using bitwise OR operator (Setting bits)

As the characteristics of bitwise OR operators, if we OR 0 with any bit then it remains as it is. If we OR 1  with any bit then it becomes 1 which is called the Setting of bits.

Let us understand this by the below example.

Ex. a = 10; // original value

And suppose the mask is 00000011. So want the result of a|00000011 so convert it into a binary. The binary representation of int is 8 bits.

a(binary) = 00001010

Now each individual bit of original value and mask will be ored to each other individually. 

a&00000011 = ( 0000 1010 )  | (  0000 0011 )  = ( 0000 1011 ) (decimal : 11 )

So the result of a|00000011 is 11. The same thing is done in the below program. Where b is the mask. And the result is printed.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=10,b=3;
  clrscr();
  printf("result : %d ",a | b);
  getch();
}

Output:

Masking using bitwise XOR operator (Testing bits)

As the characteristics of a bitwise XOR operator, if we XOR 0 with any bit then it remains as it is. If we XOR 1  with any bit then it becomes complemented. One thing is also observed in XOR that when both bits are same then output is 0 and when both bits are different then output is 1. which is called Testing of bits.

Let us understand this by the below example.

Ex. a = 10; // original value

And suppose the mask is 00000011. So I want the result of a^00000011 to convert it to a binary. The binary representation of int is 8 bits.

a(binary) = 00001010

Now each individual bit of original value and mask will be XORed to each other individually. 

a&00000011 = ( 0000 1010 )  ^ (  0000 0011 )  = ( 0000 1001 ) (decimal : 9 )

So the result of a|00000011 is 11. The same thing is done in the below program. Where b is the mask. And the result is printed.

#include<stdio.h>
#include<conio.h>
void main()
{
  int a=10,b=3;
  clrscr();
  printf("result : %d ",a ^ b);
  getch();
}

Output:

 

Learning from this Blog:

  1. What is the meaning of masking.
  2. What is the original value and what is a mask?
  3. Using how many bitwise operators, we can perform masking?
  4. What is the meaning of setting, clearing, testing of bits?
  5. How to write the program of setting, clearing, testing of bits using bitwise operators?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments