Friday, November 4, 2011

Bitwise AND Operator in C programming language

This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis. Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask. The & operator operates on a pair of bits to yield a resultant bit. The rules that decide the value of the resultant bit are shown below

This can be represented in a more understandable form as a ‘Truth Table’ shown in Figure 14.8.







The example given below shows more clearly what happens while ANDing one operand with another. The rules given in the Figure 14.8 are applied to each pair of bits one by one.



Work through the Truth Table and confirm that the result obtained is really correct.
Thus, it must be clear that the operation is being performed on individual bits, and the operation performed on one pair of bits is completely independent of the operation performed on the other pairs.
Probably, the best use of the AND operator is to check whether a particular bit of an operand is ON or OFF. This is explained in the following example.
Suppose, from the bit pattern 10101101 of an operand, we want to check whether bit number 3 is ON (1) or OFF (0). Since we want to check the bit number 3, the second operand for the AND operation should be 1 * 23, which is equal to 8. This operand can be represented bitwise as 00001000.
Then the ANDing operation would be,
10101101 Original bit pattern
00001000 AND mask
--------------
00001000 Resulting bit pattern
The resulting value we get in this case is 8, i.e the value of the second operand. The result turned out to be 8 since the third bit of the first operand was ON. Had it been OFF, the bit number 3 in the resulting bit pattern would have evaluated to 0 and the complete bit pattern would have been 00000000.
Thus, depending upon the bit number to be checked in the first operand we decide the second operand, and on ANDing these two operands the result decides whether the bit was ON or OFF. If the bit is ON (1), the resulting value turns out to be a non-zero value which is equal to the value of second operand, and if the bit is OFF (0) the result is zero as seen above. The following program puts this logic into action.
/* To test whether a bit in a number is ON or OFF */
main( )
{
int i = 65, j ;
printf ( "\nvalue of i = %d", i ) ;
j = i & 32 ;
if ( j == 0 )
printf ( "\nand its fifth bit is off" ) ;
else
printf ( "\nand its fifth bit is on" ) ;
j = i & 64 ;
if ( j == 0 )
printf ( "\nwhereas its sixth bit is off" ) ;
else
printf ( "\nwhereas its sixth bit is on" ) ;
}
And here is the output...
Value of i = 65
and its fifth bit is off
whereas its sixth bit is on
In every file entry present in the directory, there is an attribute byte. The status of a file is governed by the value of individual bits in this attribute byte. The AND operator can be used to check the status of the bits of this attribute byte. The meaning of each bit in the attribute byte is shown in  Figure




Now, suppose we want to check whether a file is a hidden file or not. A hidden file is one, which is never shown in the directory, even though it exists on the disk. From the above bit classification of attribute byte, we only need to check whether bit number 1 is ON or OFF.
So, our first operand in this case becomes the attribute byte of the file in question, whereas the second operand is the 1 * 21 = 2, as discussed earlier. Similarly, it can be checked whether the file is a system file or not, whether the file is read-only file or not, and so on.
The second, and equally important use of the AND operator is in changing the status of the bit, or more precisely to switch OFF a particular bit..
If the first operand happens to be 00000111, then to switch OFF bit number 1, our AND mask bit pattern should be 11111101. On applying this mask, we get,
00000111 Original bit pattern
11111101 AND mask
--------------
00000101 Resulting bit pattern
Here in the AND mask we keep the value of all other bits as 1 except the one which is to be switched OFF (which is purposefully kept as 0). Therefore, irrespective of whether the first bit is ON or OFF previously, it is switched OFF. At the same time the value 1 provided in all the other bits of the AND mask (second operand) keeps the
it values of the other bits in the first operand unaltered.
Let’s summarize the uses of bitwise AND operator:

(a) It is used to check whether a particular bit in a number is ON or OFF.
(b)  it is used to turn OFF a particular bit in a number

No comments:

Post a Comment