Masking

Masking implies transforming the desired bits of a variables by making use of logical operators. The operand which is used to perform masking is

called the mask. Masking is used to -
- copy a portion of a given bit pattern to a new variable and all fill the remainder bits of the variable with 0. (using &)
- copy a portion of a given bit pattern to a new variable and fill the remainder bits of the variables with 1. (using |)
- copy a portion of a given bit pattern to a new variable and invert the remainder bits of the variable. (using ^)
- to decide bit pattern of an integer variable.

Example: To use AND to check whether the third bit is ON or OFF
#define SETBIT 4
main()
{
int x,y;
printf("Enter value for y :");
scanf("%d", &y);
x = y & SETBIT;
if (x==0)
printf("\nThe third bit is OFF");
else
printf("\nThe third bit is ON");
}

No comments:

Post a Comment