Left and Right operators are used to shift bits of a variable. First it converts that variable into its binary form and then shifts it to right or left.
Using both operators, we can do multiplication and division of any variable with any number which is a power of 2. All these things we are going to know in this section.
Left-shift (<<)
This operator first converts any variable to its binary value. Then the left shifts bits of that variable “n” times and makes other bits(remaining bits at right side) with 0. That “n” is defined in syntax as written below. Blank bits on the right side are filled with 0’s.
Syntax : <variable> << <”n”, which is number of times we want to left shift bits>
Let us understand this by the below example.
a = 10;
We want the result of a<<3 so convert a in binary. The binary representation of int is 8 bits.
a(binary) = 00001010
For a<<3, all bits are shifted left 3 times and 3 bits are blank, as shown below.
0 1 0 1 0 _ _ _
These blank three bits on the right side are filled with 0 as shown below.
0 1 0 1 0 0 0 0 (decimal : 80 )
So the result of a<<3 is 80.
Below example is different operations performed on value a=12. So the binary of 12 is 00001100. We are taking the left shift for 1 time, 2 times and 3 times.
a | Binary form | Value of “a” after operation |
a << 1 | 00001100 << 1 | 00011000 |
a << 2 | 00001100 << 2 | 00110000 |
a << 2 | 00001100 << 3 | 01100000 |
Right-shift (>>)
This operator first converts any variable to its binary value. Then the right shifts bits of that variable “n” times and makes other bits(remaining bits on the left side) with 0. That “n” is defined in syntax as written below. Blank bits on the right side are filled with 0’s.
Syntax : <variable> >> <”n”, which is number of times we want to right shift bits>
Let us understand this by the below example.
a = 10;
We want the result of a>>3 so convert it to binary. The binary representation of int is 8 bits.
a(binary) = 00001010
For a>>3, all bits are shifted right 3 times and 3 bits are blank on the left side, as shown below.
_ _ _ 0 0 0 0 1
These blank three bits on the left side are filled with 0 as shown below.
0 0 0 0 0 0 0 1 (decimal : 1 )
So the result of a>>3 is 1.
Below example is different operations performed on value a=12. So the binary of 12 is 00001100. We are taking the right shift for 1 time, 2 times and 3 times.
a | Binary form | Value of “a” after operation |
a << 1 | 00001100 >> 1 | 00000110 |
a << 2 | 00001100 >> 2 | 00000011 |
a << 2 | 00001100 >> 3 | 00000001 |
In the below program, there are three int variables: a, b, c. a is having a value 4 and b is a<<2 and c is a>>2. Now,
b = a<<2
= (00000100) << 2
= 00010000
= 16
c = a>>2
= (00000100) >> 2
= 00000001
= 1
Output:#include<stdio.h>
#include<conio.h>
void main()
{
int a=4,b,c;
clrscr();
b = a<<2;
c = a>>2;
printf("b = %d , c =%d ",b,c);
getch();
}
Division and multiplication with 2n using left-shift and right-shift operators
Left shifting bits of any number n times, it is equivalent to multiply that number with 2n. And right shifting bits of any number n times, it is equivalent to divide that number by 2n. Let us understand it by example.
a = 8 ; // binary 00001000
a << 1 = 00001000 << 1 = 00010000 = 16 in decimal (which is 8 * 21)
a >> 1 = 00001000 << 1 = 00000100 = 4 in decimal (which is 8 / 21)
Below is the same program for this explanation.
#include<stdio.h> #include<conio.h> void main() { int a=8; int b,c; clrscr(); b = a << 1; c = a >> 1; printf("b = %d , c = %d ",b,c); getch(); }
Output:
Learning from this blog:
- Use of left-shift and right-shift operators.
- How both operators work.
- Syntax of using left-shift and right-shift operators.
- How to perform multiplication and division using shift operators.
- How to write the program using left-shift and right-shift operators.