Full Stack Developer
The left shift operator is a bitwise shift operator in C which operates on bits. It is a binary operator which means it requires two operands to work on. Following are some important points regarding the Left shift operator in C:
Syntax:
The syntax for the left shift operator in C is as follows:
variable_name<<number_of_positions
In the above statement, there are two values; the first one is an integer variable on which we want to apply the left shift operator. The name of this variable can be any name given by the user. The second value is a number that specifies the number of positions a user wants to shift the bits to the left.
Left shift driver requires two operands to work on. Both the operands of the left shift driver should be of integral type. It shifts the bits of the first operand to the left by the number of positions specified by the alternate operand. Contemporaneously, the empty spaces created by the bits shifted to the leftism are also filled with zeroes. Including the sign bit, the bits which are shifted off to the end are also discarded.
Following are some cases when the result of left shift operation will be undefined:
Thus, left shift operation will be performed when both the operands are positive.
On the other hand, if the value of the second operand is zero then the left shift operation will not be performed. Such as the result of 40<<0 will be equal to 40 itself.
Let us now understand the working of the left shift operator with the help of an example. In this example, we will take a decimal number say 40. The binary equivalent of 40 is 101000. We will perform a left shift operation on this binary value.
In the above example, we can see that on performing left shift operation on a binary value all its bits have been shifted to the left and the space created on the right side is filled with zero.
Thus, the value of 40<<1 is 01010000. The decimal equivalent of this binary value is 80.
Example showing left shift operation performed on two positive operands.
Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a = 40;
int b = 0;
printf("\n Enter number of positions for the bits to shift to the left : ");
// accepting the value of second operand from the user
scanf("%d", &b);
printf("The result of left shift operation is : ");
// Binary value of 40 is 101000
printf("\n %d << %d = %d", a, b, a<<b);
}
Output:
Example showing a scenario when the value of the second operand is negative.
Code:
#include<stdio.h>
#include<conio.h>
main()
{
int result = 0;
result = 40 << -1;
printf("The result of left shift operation is : ");
// Binary value of 40 is 101000
printf("\n 40 << -1 = %d", result);
}
Output:
Example showing scenario when the value of the first operand is negative.
Code:
#include<stdio.h>
#include<conio.h>
main()
{
int result = 0;
result = -40 << 1;
printf("The result of left shift operation is : ");
// Binary value of 40 is 101000
printf("\n -40 << 1 = %d", result);
}
Output:
Example showing scenarios when several positions to be shifted is zero and greater than the size of integer.
Code:
#include <stdio.h>
#include <conio.h>
main()
{
int a = 0;
int b = 0;
int result1 = 0, result2 = 0;
printf("\n Enter the number : ");
// accepting the value of first operand from the user
scanf("%d", &a);
result1 = a << 0;
result2 = a << 34;
printf("The result of left shift operation is : ");
printf("\n %d << 0 = %d", a, result1);
printf("\n %d << 34 = %d", a, result2);
}
Output: