Shift Operators:
- The bitwise 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);
}
Shift operators have higher priority 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