Friday, November 4, 2011

Bitwise XOR Operator in C programming

The XOR operator is represented as ^ and is also called an Exclusive OR Operator. The OR operator returns 1, when any one of the two bits or both the bits are 1, whereas XOR returns 1 only if one of the two bits is 1. The truth table for the XOR operator is given below.






XOR operator is used to toggle a bit ON or OFF. A number XORed with another number twice gives the original number. This is shown in the following program.
main( )
{
int b = 50 ;
b = b ^ 12 ;
printf ( "\n%d", b ) ; /* this will print 62 */
b = b ^ 12 ;
printf ( "\n%d", b ) ; /* this will print 50 */
}

No comments:

Post a Comment