Left Shift Operator in C

The left shift operator is basically a bitwise operator used in C that operates on the bits. This operator is binary in nature- which means that it needs two of the operands for working. We represent it by the << sign.

In this article, we will take a closer look at the Left Shift Operator in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Use of the Left Shift Operator in C

We use the left shift operator to shift the bits of available values to the left. It does so by adding zeros to the right side of the value in the empty spaces that get created due to shifting. It shifts the bits available for the first operand to the left on the basis of the number of positions that the second operand specifies.

Syntax of Left Shift Operator in C

Here is the syntax of this operator in the C language:

variable_name<<number_of_positions

As you can see, the above statement has two values. The first one here is a variable in the form of an integer, and we would like to apply the left shift operator on this. A user can give any name to this variable. Besides this, the second given value refers to a number. This number specifies the total number of positions that the bits must be shifted to the left according to a user.

Working of a Left Shift Operator in C

This operator requires two of the operands to work in a code. Both of the operands used in the left shift operator must be integral in nature. It is because this operand shifts the bits available from the first operand to the left on the basis of the number of positions that the second operand has specified. Simultaneously, it fills the empty spaces created by shifting bits with zeroes (0). Then, the bits that get shifted off to the very end, along with the sign bit, get discarded by the operator.

Undefined Result Of Operator

Here are some of the cases where left shift operation would lead to an undefined result:

  • In case the value of the first available operand is negative, the result generated from the left shift operation will turn out to be undefined.
  • In a very similar case, when the value of the second operand is negative, then also we get an undefined result.
  • Added to this, we also get undefined results when the value of the second operand is equal to or greater than the total number of bits present in the first operand.

In short, the left shift operator would only work when both the operands available with us are positive. But, in case the second operand’s value is zero (0), then this operand won’t be able to function. It means that the result of any pair, say 30 << 0 is going to be 30 itself.

We will take a closer look at the working of this operator. Let us take the decimal number 40 here. 101000 is the binary equivalent of the number 40. So let us perform the left shift operation on the binary value of this decimal.

In this example, you can clearly notice that all the bits have shifted to the left once we performed the left shift operation on the binary value. The empty space thus created is now filled with zeroes (0).

Thus, the value obtained from 40 << 1 is 01010000, and 80 is the decimal equivalent of this binary value.

Note: Whenever we perform this operation on an integer x with an integer y, the result obtained after the operation is always going to be equal to the multiplication of x with 2^y.

In the example mentioned above, x = 40 and y = 1. Thus, the result obtained from x<<y, i.e., 40<<1 will be 40 * 2^1, which will be equal to 80.

Example of Left Shift Operator in C

Let us take a look at an example of how we use the left shift operator in C in a given code.

#include <stdio.h>

int main() {

int a = 28; // 11100

int b = 0;

for(b;b<=3;++b)

printf(“The left shift obtained by %d: %d\n”, b, a<<b);

return 0;

}

The output obtained in this case would be:

The left shift obtained by 0: 28

The left shift obtained by 1: 56

The left shift obtained by 2: 112

The left shift obtained by 3: 224

Some Important Points to Remember

1. Never use a left shift operator for negative numbers. The result obtained would be undefined if either of the operands has a negative value. For instance, the results obtained from both the operations -2 << 2 and 2 << -2 are going to be undefined.

2. If we try to shift the given number/ integer more than its actual size, then the behaviour will be undefined. For instance, operation 1 << 33 will produce an undefined result if the integers get stored using 32 bits. We use the ULL (Unsigned Long Long) in case of a bit shift of comparatively larger values. It is defined using 64 bits, and can thus store the large values within.

Practice Problems on the Left Shift Operator in C

1. What would be the result obtained by using a left shift operator on -5 << 2?

A. 20

B. 0

C. Undefined

D. 11

Answer – C. Undefined.

One of the operands is a negative integer. In such a case, we would always get an undefined result.

2. What would be the result obtained by using a left shift operator on 40 << 34?

A. 5

B. 160

C. 30

D. Undefined

Answer – B. 160

3. What would be the result obtained by using a left shift operator on 80 << 0?

A. Undefined

B. 11

C. 0

D. 80

Answer – D. 80

In case the second operand’s value is zero (0), then this operand won’t be able to function. It means that the result of this pair, 80 << 0 is going to be 80 itself.


FAQs

What would happen if the size of the operand we used is larger than the size of the concerned operator in the code?

The result obtained from the left shift operation in such a case would be undefined behavior, and we will receive a warning on the screen.

What would happen in such scenarios where the number of positions that we want to be shifted is 0 (zero) and the value of the integer is positive?

The result would be the same as the value of the integer. For example, if the operation is performed on 30 << 0, the result is going to be 30.

Can we use the left shift operator with negative values? If not both, at least one of the operands?

Never use a left shift operator for negative numbers. The result obtained would be undefined if either of the operands has a negative value. For instance, the results obtained from both the operations -2 << 2 and 2 << -2 are going to be undefined.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

 

Leave a Comment

Your Mobile number and Email id will not be published.

*

*