Shift Operators in C Language

This “Shift Operators in C Language” post is providing complete information about all the shift operators which are available in C language with suitable examples.


 

Shift Operators:

  • The bit wise shift operators move the bit values in the memory location .

  • The left operand specifies the value to be shifted.

  • The right operand specifies the number of positions that the bits in the value are to be shifted.

  • The bit shift operators take two arguments, and looks like:

          x << n
          x >> n

  • Where x can be any kind of int variable or char variable and n can be any kind of int variable.

 

#include<stdio.h>
main()
{
short int a=8,b,c;
b = a << 2 ;
c = a >> 2 ;
printf(“[%d][%d]”,b,c);
}

 

Note : shift operators having higher prior than relational operators.
#include<stdio.h>
main()
{
int a=2,b=8,c,d;
c = a << 2 == 8 ;
d = b >> 2 == 2 ;
printf(“%d\n%d\n”,c,d);
}

Theoretical formulas to shift data bits :
   >>  –>  n/2^s (n divided by 2 power s)
    <<  –>  n*2^s (n multiplied by 2 power s)

    where    “n” represents data
“s” represents bits to be shifted

Share this