What are Operators ?

Bitwise Operators in C

Bitwise operators perform manipulations of data at the bit level. These operators also perform the shifting of bits from right to left. Bitwise operators are not applied to float or doublelong doublevoid, etc.

The following table contains the bitwise operators. There are 6 bitwise operators in the C language.

&Bitwise AND 
|Bitwise OR 
^Bitwise Exclusive OR (XOR) 
~One’s complement (NOT) 
>>Shift right 
<<Shift left 

The bitwise AND, OR, and NOT operator works the same way as the Logical AND, OR, and NOT operators, except that the bitwise operators work bit by bit.

Below we have a truth table for showing how these operators work with different values.

aba & ba | ba ^ b

Bitwise operators can produce any arbitrary value as result. It is not mandatory that the result will either be 0 or 1.

Bitwise >> and << operators

The bitwise shift operator shifts the bit value, either to the left or right. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.

Understand, how bits shift from left to right and vice versa.

a = 00010000
b = 2
a << b = 01000000 
a >> b = 00000100


In case of a << b2 bits are shifted to left in 00010000 and additional zeros are added to the opposite end, that is right, hence the value becomes 01000000

And for a >> b2 bits are shifted from the right, hence two zeros are removed from the right and two are added on the left, hence the value becomes 00000100

Please note, shift doesn’t work like rotating, which means, the bits shifted are not added at the other end. The bits that are shifted are lost.

Bitwise One’s Complement (~) Operator

The one’s complement operator, will change all the 1’s in the operand to 0, and all the 0’s are set to 1.

For example, if the orginal byte is 00101100, then after one’s complement it will become 11010011.

Example: Bitwise Left & Right shift Operators

Let’s see an example to understand the bitwise operators in C programs.

#include <stdio.h>

int main() {

   int a = 0001000, b = 2, result;

   // <<
   result = a<<b;
   printf("a << b = %d \n",result);

   // >>
   result = a>>b;
   printf("a >> b = %d \n",result);

   return 0;



a << b = 2048 a >> b = 128

Leave a Reply

Your email address will not be published. Required fields are marked *