C - Bitwise Operators

<<Previous

Next >>





C provides six bitwise operators that operates up on the individual bits in the operand. A bit pattern consists of 0's and 1's. These bitwise operators may be applied only to the char and integer operands. They may not be applied on the other data types like float,double or void. The following table shows the name and meaning of the bitwise operators.

Operators    Meaning       
>> Right Shift
<<Left Shift
^ Bitwise XOR (Exclusive OR)
~One's complement(NOT)
&Bitwise AND
|Bitwise OR

If we want to compare two decimal values by using the bitwise operator, then that decimal values should be first converted into binary form and then the bitwise comparison will be taking place.

Bitwise AND, OR, XOR and NOT

The &, |, ^ and ~ operators can perform boolean logical operations. The NOT (~) operator is a unary which can simply perform the conversion that is 0's into 1's and vice-versa. The other three operators are the binary operators. Those binary operators can follow some rules to perform the boolean logical operations. The following is the truth table of AND(&), OR(|) and XOR(^):

   A      B      A&B      A|B      A^B   
000 0 0
01 0 1 1
10 0 1 1
111 1 0

For example:

Consider the following 8-bit example which have two integer values:
     A= 10, and B =8.
The binary form of these values are given below.

  A=00001010
  B=00001000

Now we have to apply the bitwise operations on them.

Bitwise AND (&):
This operator sets the bit to 1 if both corresponding bits in A and B are  one.

A&B------->   00001010
              00001000
Result----->  00001000 -----(Decimal value is 8)

Bitwise OR (|):
This operator sets the bit to 1 if either or both corresponding bits in A and B are one.

A|B------->  00001010
             00001000
Result-----> 00001010 ----(decimal values is 10)

Bitwise XOR (^):
This operator sets the bit to 1 if either, but not both, corresponding bits in A and B are one.

A^B-------->     00001010
                 00001000
Result------->   00000010----(decimal value is 2)

Bitwise NOT (~):
This operator performs one's complement that means, convert a 0 bit to 1 and  a 1 bit to 0.

 A      00001010
~A     11110101 ----(decimal value is 245)

Bitwise Shift operators

The bitwise operators shift the bits of an integer variable to the left or right respectively. The C supports two kinds of shift operators.

  1. Left Shift
  2. Right Shift

Left shift

 Syntax:
          x<<n

Here the bits in x are shifted n bit position to the left and the vacated bits are being filled with zeros.

For example:
		int x= 4;       /* equal binary values of 4 is 00000100 */
		int y=x<<2;  /* equal binary values left shift is 00010000 (decimal 16 is assigned into y ) */

Left shift can use the form to get the output that is input number is multiplied by 2s

  			Output(Y) = number(n) * 2s
			where s = Number of position to be shifted
          	              n = Input number
          		      Y = Output number

Right Shift

Syntax:
			x>>n

Here the bits in x are shifted n bit position to right and the vacated bits are being filled with zero.

For example:
			int x=8;   	 /* equal binary value is 00001000 */
			int y=x>>2;  /* equal right shifted value is 00000010 (decimal 2)*/

Therefore, the right shift is done by using input number is to be divided by 2s

			Output(Y) = Input number(n) / 2s
			where s = Number of position to be shifted
          		      n = Input number
                  	      Y = Output number

Following is an example C Program using Bitwise Operators.

#include <stdio.h>
#include <conio.h>

int main()
{
   int A=5; /*binary equal 00000101*/
   int B=3; /*binary equal 00000011 */
   int C;
   /*Bitwise AND */
   C = A&B;
   printf("The Bitwise AND is :%d \n",C);

   /*Bitwise OR */
   C = A|B;
   printf("The Bitwise OR is :%d \n",C);

   /*Bitwise XOR */
   C = A^B;
   printf("The Bitwise XOR is : %d \n ",C);

   /*Bitwise NOT */
   C = ~A;
   printf("The bitwise NOT is : %d \n ",C);

   /*Bitwise Left Shift */
   C = A<<2;
   printf("The Bitwise Left Shift is :%d \n ",C);

   /*Bitwise Right Shift*/
   C = A>>2;
   printf("The Bitwise Right Shift is :%d \n ",C);

	 return(0);
 }
 

Output:

The Bitwise AND is : 1
The Bitwise OR is : 7
The Bitwise XOR is : 6
The Bitwise NOT is : 250
The Bitwise Left shift is : 20
The Bitwise Right Shift is : 1

<< Previous

Next >>