Check The Programming Section
C/C++ supports two ways of shifting the bits of a number using two operators
right shift (>>)
left shift (<<)
The right shift operator is represented by placing two greater than symbols together (>>). It requires two operands. Consider the example given below:
int a = 6>>2;
The >> operator shifts all bits of left operand from left to right direction by 2 positions. Similarly, 6>>3, shifts all bits of 6 to right direction upto 3 place. The number of places the bits are shifted depends on the number following the operator (i.e. its right operand). After shifting the all bits by a specified number of position, the leading bits needs to be filled with zero (0). Consider an example depicted in the figure:
In the above figure the shifting of bits is represented by two positions and indicates by red colored arrow. After shifting of bits blanks are created to the left most bits marked with black color. These blank bit positions are always filled with zero.
6>>2 = 1
The right shifting of 6 by 2 positions gives us the binary 0000000000000001 which is equal to decimal 1. The shifting to bits to right is a operation of divission by 2, to the left operand of right shift operator, by ignoring the reminder. Consider the below illustrations:
6/2 = 3
3/2 = 1
The divission operation will be repeated a number of times (2), equal to the number of position that we want to shift (2). What happens then, when we shift the bits of a odd number? Consider the below illustration:
Example 1:
31>>1 is 15
31/2 = 15 (ignoring the reminder 1)
Example 2:
25>>1 is 24
25/2 = 12 (ignoring the reminder 1)
#include<stdio.h>
void getBits(short int num){
int i = 15;
int bin[16] = {0};
while(num>0){
bin[i] = num%2;
i--;
num/=2;
}
for(int i=0;i<=15;i++){
printf("%d",bin[i]);
}
}
int main(){
short int num = 31, k;
printf("The decimal %d is same as binary ",num);
getBits(num);
k = num>>1;
printf("\nAfter right shift by 2 bits position.....\n");
printf("The decimal %d is same as binary ",k);
getBits(k);
}
Left shift operator is just opposite of the right shift operator. In this case, entire bits are shifted to the left direction. And for each shifted bit, a 0 is added to the right of the number. Left shifting of operand 4 by 2 positions gives us 16. A left shift operation is a multiplication operation of its left operand 4 by 2. Consider the illustration given below:
4<<2 is 16
By multiplying the number by 2
4 X 2 = 8
8 X 2 = 16
#include<stdio.h>
void getBits(short int num){
int i = 15;
int bin[16] = {0};
while(num>0){
bin[i] = num%2;
i--;
num/=2;
}
for(int i=0;i<=15;i++){
printf("%d",bin[i]);
}
}
int main(){
short int k = 21;
for(int i=0;i<=5;i++){
printf("%d left shift with %d gives ",k, i);
getBits(k<<i);
printf("\n");
}
}
21 left shift with 0 gives 0000000000010101
21 left shift with 1 gives 0000000000101010
21 left shift with 2 gives 0000000001010100
21 left shift with 3 gives 0000000010101000
21 left shift with 4 gives 0000000101010000
21 left shift with 5 gives 0000001010100000