twitter
    Find out what I'm doing, Follow Me :)

The Logical Functions

Load and display the program bitops.c.

main( )
{
char mask;
char number.6,;
char and,or,xor,inv,index;
number[0] = 0X00;
number[1] = 0X11;
number[2] = 0X22;
number[3] = 0X44;
number[4] = 0X88;
number[5] = 0Xff;
printf("nmbr mask and or xor inv\n");
mask = 0X0F;
for (index = 0;index <= 5;index++) {
and = mask & number[index];
or = mask l number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",number.index.,
mask,and,or,xor,inv);
}
printf("\n");
mask = 0X22;
for (index = 0;index <= 5;index++) {
and = mask & number[index];
or = mask l number[index];
xor = mask ^ number[index];
inv = ~number[index];
printf("%5x %5x %5x %5x %5x %5x\n",number[index],
mask,and,or,xor,inv);
}
}

The functions in this group of functions are used to do bitwise operations, meaning that the operations are performed on the bits as though they were individual bits. No carry from bit to bit is performed as would be done with a binary addition. Even though the operations are performed on a single bit basis, an entire byte or integer variable can be operated on in one instruction. The operators and the operations they perform are given in the following table;

& Logical AND, if both bits are 1, the result is 1.
| Logical OR, if either bit is one, the result is 1.
^ Logical XOR, (exclusive OR), if one and only one bit is 1, the result is 1.
~ Logical invert, if the bit is 1, the result is 0, and if the bit is 0, the result is 1.

The example program uses several fields that are combined in each of the ways given above. The data is in hexadecimal format. It will be assumed that you already know hexadecimal format if you need to use these operations. If you don’t, you will need to study it on your own. Teaching the hexadecimal format of numbers is beyond the scope of this tutorial.  
Run the program and observe the output.