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");
}

Bitwise Complement (~) Operators

This operators is also called one's complement operator. This is an unary operator and inverts all the bits represented by the operand. i.e. if a bit

is 0 it becomes 1 and if it is 1 it becomes 0.
Example:
x = 1000 1100 0110 1010
~x=0111 0011 1001 0101
The use of this operator with AND is to turn of a particular bit.

Bitwise Shift Operators (<<,>>)

The bitwise shift operators are used to shift the bits either to the left or to the right. The shift operator are : Left Shift (<<) and right shift

(>>). The general form of bitwise shift operators is:
op << n for left shift
op >> n for right shift
Here op is an integer expression which is to be shifted by n bit positions to the left or right. The left shift operator causes all the bits in op to be

shifted left by n position. The n leftmost bits of the original bit pattern are lost, and the n rightmost bits are filled with 0s.

Bitwise Exclusive OR (^)

The result of exclusive OR is 1 only if either of the bits are 1 or 0 otherwise it is 0. In our above example the result of a

exclusive ORed with b is:
a ^ b = 0000 0000 0001 1010

Bitwise OR (|)

Considering the value of the a and b as given above, and making using of the table the result of bitwise OR on and b will be:
a | b = 0000 0000 0001 1110
Bitwise OR is used to set a particular bit to 1.

Bitwise AND (&)

The bitwise AND operand takes both values of operands as integers. if any of the bits is 0, or both the bits are 0 then the ANDing gives a 0. If both

the bits are 1, the result of bitwise AND is a 1. eg. 1=10 and b=20 then their binary representation is:
a = 0000 0000 0000 1010
b = 0000 0000 0001 0100
then a & b= 0000 0000 0000 0000
The resulting value represent the number 0. The use of bitwise AND is to determine whether a particular bit is a 1 or a 0 in programming situations.

Bitwise Logical Operators

There are three bitwise logical operators as we already know. They are:
bitwise AND (&)
bitwise OR (|)
bitwise exclusive OR (^)
These operators are known as binary operators. They can operate on two operands of type integer. The operators work on these integers bit by bit bit

starting from the rightmost (the least significant).
Bitwise operators have a lower precedence than relational operators.