What is Left Shift Operator in C?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

What is Left Shift Operator in C?

The Left Shift Operator operates on the binary bits. Because the left shift operator is a binary operator, we'll need two operands to shift the bits to the left side and add zeroes to the space on the right side once they've been shifted. It is denoted by the << sign.

Syntax

The left shift operator's result will be the same as multiplying varName by 2no_of_position2^{no\_of\_position}.

Example

As we can see in the example above, multiplying 100 by 232^3 equals 800.

What Is The Use of Left Shift Operator in C?

We use the left shift operator to move the bits of available values to the left. It performs this by putting zeros on the right side of the value in the empty spaces created by shifting. Based on the number of positions specified by the second operand, it shifts the bits available for the first operand to the left.

How Left Shift Operator Works in C?

To use a left shift operator, we need two operands. The integer type should be used for both operands of the left shift operator. The first operand's bits are shifted to the left by the number of places given by the second operand. Simultaneously, the zeroes are placed into the empty spaces generated by the bits shifted to the left. The bits shifted to the end, including the sign bit, are discarded.

Examples to Implement Left Shift Operator in C

Output:

Example Showing Left Shift Operation Performed on Two Positive Operands

Output:

Example Showing A Scenario When The Value of Second Operand Is Negative

Output:

Example Showing Scenario When The Value of First Operand Is Negative

Output:

Example Showing Scenarios When Number of Positions to be Shifted Is Zero and Greater Than The Size of Integer

Output:

Program to Use The Left Shift Operator on the unsigned int Data-type.

Output:

Undefined Result Of Operator

The following are some examples of undefined results from left shift operations:

  • If the first available operand is negative, the result of the left shift operation will be undefined.
  • In a similar scenario, if the second operand's value is negative, the result is also undefined.
  • Furthermore, we get undefined results when the second operand's value is equal to or larger than the total number of bits contained in the first operand.

what-is-left-shift-operator-in-c-1

As you can see in the diagram above, shifting the number one position to the left often results in the output being number2number * 2. All bits in this example changed to the left after we performed the left shift operation on the binary value. The resulting space is now filled with zeros (0).

In the above example, x=100x = 100 and y=1y = 1. As a result, the result produced from x<<yx<<y, i.e. 100<<1100<<1, will be 10021100 * 2^1, which is 200200.

Learn More

Learn more about bitwise shift operator in C.

Conclusion

  • We have discussed the left shift operator in C.
  • We have discussed three different aspects of left shift operator in C.
  • We covered some examples to understand the left shift operator in C.
  • We have also covered how the left shift operator works in C.