# What is Left Shift Operator in C? | How It Works?

Written by Shuvhojit DebFebruary 10, 2022
C
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Shuvhojit Deb

Full Stack Developer

## Originations of Left Shift Operator in C

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:

• It is represented by the ‘<<’ sign.
• It is used to shift the bits of a value to the left by adding zeroes to the empty spaces created at the right side after shifting.
• The bits of the first operand are shifted to the left by the number of positions specified by the second operand.

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.

## Working of Left Shift Operator in C

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:

• If the value of the first operand is negative, then the result of the left shift operation will be undefined.
• Similarly, if the value of the second operand is negative or if it is greater than or equal to the number of bits in the first operand, then the result of the left shift operation will be undefined.
• The result of the left shift operation will also be undefined if the value of the second operand is greater than the size of the integer.

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.

## Implementation of Left Shift Operator in C

### Example #1:-

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 #2:-

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 #3:-

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 #4:-

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:

## Conclusion

1. The left shift operator is a logical bitwise operator.
2. It is a binary operator that operates on two positive integral operands.
3. It shifts the bits to the left by the number of positions specified by its second operand.
4. Empty spaces created on the right are filled with zeroes.
C Language
Programming
Left Shift
Operators
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
You must be Logged in to comment
Code Block
Shuvhojit Deb
Full Stack Developer
117 Blog Posts
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.