## Introduction to Left Shift Operator in C

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 Left shift operator in C:

- It is represented by ‘<<’ 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 first operand are shifted to the left by the number of positions specified by the second operand.

**Syntax:**

The syntax for 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 left shift operator. The name of this variable can be any name given by the user. The second value is a number which specifies the number of positions a user wants to shift the bits to the left.

### How Left Shift Operator Works in C?

Left shift operator requires two operands to work on. Both the operands of the left shift operator should be of integral type. It shifts the bits of the first operand to the left by the number of positions specified by the second operand. Simultaneously, the empty spaces created by the bits shifted to the left are then filled with zeroes.Including the sign bit, the bits which are shifted off to the end are then discarded.

Following are some cases when the result of left shift operation will be undefined:

- If the value of first operand is negative, then the result of left shift operation will be undefined.
- Similarly, if the value of 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 left shift operation will be undefined.
- The result of left shift operation will also be undefined if the value of second operand is greater than the size of integer.

Thus, left shift operation will be performed when both the operands are positive.

4.5 (4,906 ratings)

View Course

On the other hand, if the value of second operand is zero then 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 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 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 empty 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.

**Note:**Whenever we will perform left shift operation on an integer ‘a’ with an integer ‘b’. The result of the operation will always be equal of multiplying ‘a’ with ‘2^b’. In the above case a = 40 and b = 1. Thus, the result of a<<b i.e. 40<<1 is ’40 * 2

^{1}’ which will be 80.

### Examples to Implement Left Shift Operator in C

Below are the examples 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 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:**

Along with this there is a warning in the program for the line highlighted in yellow.

**Below is the Warning:**

We got this warning because our second operand is negative.

#### Example #3

Example showing scenario when the value of 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:**

Along with this there is a warning in the program for the line highlighted in yellow.

**Below is the Warning:**

We got this warning because our first operand is negative.

#### Example #4

Example showing scenarios when number of 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:**

Along with this, there is a warning in the program for line highlighted in yellow.

**Below is the Warning:**

We got this warning because the size of the operand is greater than the size of an integer.

### Conclusion

- The left shift operator is a logical bitwise operator.
- It is a binary operator that operates on two positive integral operands.
- It shifts the bits to the left by the number of positions specified by its second operand.
- Empty spaces created in the right are filled with zeroes.

### Recommended Articles

This is a guide to Left Shift Operator in C. Here we discuss the Introduction of Left Shift Operator in C and how it works along with different Examples and its Code Implementation. You can also go through our other suggested articles to learn more –