Bitwise Operators

The bitwise operators available in C are as follows:

1. & : Bitwise AND Operator
2. | : Bitwise OR Operator
3. ^ : Bitwise XOR Operator
4. ~ : One�s Complement Operator

Bitwise operators are different from logical operators because these operators allow the programmers to operate on individual bits within the data. Logical operators are generally used to combine two or more conditions in an if statement or while statement.

Bitwise AND Operator (&)

The bitwise AND operator is used to compare two operands on a bit-by-bit basis. The resultant bit is generated according to the following rules.

 A B A & B 0 0 0 0 1 0 1 0 0 1 1 1

Here, A represents a bit from the first variable and B represents a bit from the second variable. The bitwise AND operator returns 1 only when both the bits to be compared are 1.

Syntax:   k = i & j;
where   k => resultant
i and j => variables being operated on

The following example sheds some light on how the bitwise AND operator works. Considering two character variables A & B containing values which may be represented in bitwise format as follows:

 1 0 0 0 1 1 0 1

&

 0 0 1 0 1 0 0 1

gives

 0 0 0 0 1 0 0 1

C is a character variable storing the results of A & B

The bitwise AND operator operates on individual bits of two variables which must be of the same type (int or char). Bitwise AND does not work on float or double variables. The resultant stores the generated bits in the same order as in the originals. This means when the 0th bit of A is ANDed with the 0th bit of B, the result is stored in the 0th bit of C.

The best use of bitwise AND operator is to determine whether a bit is ON or OFF.
For example: if we have to check whether the 3th bit of a character variable 'c' storing value 'j' is ON or OFF, the following procedure may be used.
ASCII value of 'j' is 106 which is 01101010 in binary.
To check the status of the 3th bit, we set another variable, let say 'check', to value 8 (1 * 2^3).
When the two character variables are ANDed, the result becomes:

01101010     value of c
00001000     value of check
-----------
00001000     value of result

In this case, we get the result 8 which is the value of check. Thus, it can be seen that, if the bit to be checked is ON then the resultant has the same value as check, whereas if it is OFF, resultant gets the value 0.

Bitwise OR Operator (|)

The bitwise OR operator is used to compare two operands on a bit-by-bit basis. The resultant bit is generated according to the following rules.

 A B A | B 0 0 0 0 1 1 1 0 1 1 1 1

Here, A represents a bit from the first variable and B represents a bit from the second variable. The bitwise OR operator returns 1 when one of the bits to be compared is 1.

Syntax:   k = i | j;
where   k => resultant
i and j => variables being operated on

The following example sheds some light on how the bitwise OR operator works. Considering two character variables A & B containing values which may be represented in bitwise format as follows:

 1 0 0 0 1 1 0 1

|

 0 0 1 0 1 0 0 1

gives

 1 0 1 0 1 1 0 1

C is a character variable storing the results of A & B

The bitwise OR operator operates on individual bits of two variables which must be of the same type (int or char). Bitwise OR does not work on float or double variables. The resultant stores the generated bits in the same order as in the originals. This means when the 0th bit of A is ORed with the 0th bit of B, the result is stored in the 0th bit of C.

The best use of bitwise OR operator is to turn a bit in a variable ON.
For example: if we want to turn the 2nd bit of a character variable 'c' storing value 'j' ON, the following procedure may be used.
ASCII value of 'j' is 106 which is 01101010 in binary.
To turn the 3th bit ON in 'c', we set another variable, let say 'set', to value 4 (1 * 2^2).
When the two character variables are ORed, the result becomes:

01101010     value of c
00000100     value of set
-----------
01101110     value of result

In this case, we see that the bit at the 2nd position is turned ON (i.e. changed to 1). In 'set', only the bit which has to be turned ON in the resulting value is set to 1 while all other bits are set to zero.

Bitwise XOR Operator (^)

The bitwise XOR or Exclusive OR operator is used to compare two operands on a bit-by-bit basis. The resultant bit is generated according to the following rules.

 A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0

Here, A represents a bit from the first variable and B represents a bit from the second variable. The bitwise XOR operator returns 1 only when 1 of the bits to be compared is 1.

Syntax:   k = i ^ j;
where   k => resultant
i and j => variables being operated on

The following example sheds some light on how the bitwise XOR operator works. Considering two character variables A & B containing values which may be represented in bitwise format as follows:

 1 0 0 0 1 1 0 1

^

 0 0 1 0 1 0 0 1

gives

 1 0 1 0 0 1 0 0

C is a character variable storing the results of A & B

The bitwise XOR operator operates on individual bits of two variables which must be of the same type (int or char). Bitwise XOR does not work on float or double variables. The resultant stores the generated bits in the same order as in the originals. This means when the 0th bit of A is XORed with the 0th bit of B, the result is stored in the 0th bit of C.

The XOR operator is used to toggle a bit ON or OFF. A number XORed with another number twice gives the original number.

One's Complement Operator (~)

The one's complement operator complements the bit values of the variable it is used on, i.e. it turns the 1's present in the variable to 0's and the 0's to 1's. The one's complement operator works on the binary equivalent of the number.

For Example: one's complement of 0101 is 1010.

Syntax:   k = ~i;
where   k => resultant
i => variable being operated on

Furthur example show the working of the one's complement operator. Considering the character variables A containing the value which may be represented in bitwise format as follows:

 1 0 0 0 1 1 0 1

on performing one's complement operation (~)

 0 1 1 1 0 0 1 0

The above bit pattern shows the result of ~A.