Table of contents
1.
Introduction
2.
What is Left Shift Operator in C?
3.
Example
4.
What Is The Use of Left Shift Operator in C?
5.
How Left Shift Operator Works in C?
6.
Examples to Implement Left Shift Operator in C
6.1.
Example Showing Left Shift Operation Performed on Two Positive Operands
6.2.
Example Showing A Scenario When The Value of Second Operand Is Negative
6.3.
Example Showing Scenario When The Value of First Operand Is Negative
6.4.
Example Showing Scenarios When Number of Positions to be Shifted Is Zero and Greater Than The Size of Integer
7.
Program to Use The Left Shift Operator on the unsigned int Data-type
8.
Undefined Result Of Operator
9.
Frequently Asked Questions
9.1.
What happens when you left shift a number by a negative value?
9.2.
Can you left shift a negative number?
9.3.
What is the time complexity of the left shift operator?
10.
Conclusion
Last Updated: Nov 25, 2024
Easy

Left Shift Operator in C

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The left shift operator (<<) is a fundamental concept in C language that helps you shift the bits of an integer value to the left by a specified number of positions. It's a very powerful feature that has many different applications in low-level programming, bitwise operations, and the manipulation of binary representations. 

Left Shift Operator in C

In this article, we'll understand what the left shift operator is and how it works, and we'll see examples of how to use it effectively in our C programs. 

What is Left Shift Operator in C?

The left shift operator (<<) in C is a bitwise operator that shifts the bits of the first operand to the left by the number of positions specified by the second operand. It effectively multiplies the first operand by 2 raised to the power of the second operand.

The syntax of the left shift operator is:

(operand1 << operand2)

 

Here, operand1 is an integer value whose bits are to be shifted, and operand2 is an integer value that specifies the number of positions to shift the bits to the left.

When the left shift operation is performed, the bits of operand1 are shifted to the left by the number of positions specified by operand2. The leftmost bits that are shifted beyond the size of the integer are discarded, and the vacant positions on the right are filled with zeros.

For example, if we have the expression (5 << 2), the binary representation of 5 is 0000...0101. When we shift the bits to the left by 2 positions, it becomes 0000...010100, which is equivalent to 20 in decimal. Thus, (5 << 2) evaluates to 20.

It's important to note that the left shift operator multiplies the first operand by 2 raised to the power of the second operand. So, (5 << 2) is equivalent to (5 * 2^2), which is (5 * 4) = 20.

Let's look at an another simple example to understand the syntax better:

#include <stdio.h>
int main() {
    int num = 10;
    int shift = 3;
    int result = num << shift;  
    printf("Original number: %d\n", num);
    printf("Number of positions to shift: %d\n", shift);
    printf("Result after left shift: %d\n", result);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code


In this example, we have an integer variable num with a value of 10 and another integer variable shift with a value of 3. We use the left shift operator to shift the bits of num to the left by 3 positions and store the result in the result variable.

The output of this program will be:

Original number: 10
Number of positions to shift: 3
Result after left shift: 80


The left shift operation (10 << 3) shifts the bits of 10 (0000...1010) to the left by 3 positions, resulting in (0000...1010000), which is equivalent to 80 in decimal.

Example

Let's take a closer look at an example that shows the use of the left shift operator in C. We'll write a program that takes an integer input from the user, performs a left shift operation by a specified number of positions, and displays the result.

#include <stdio.h>
int main() {
    int num, shift, result;
    printf("Enter an integer value: ");
    scanf("%d", &num);
    printf("Enter the number of positions to shift left: ");
    scanf("%d", &shift);
    result = num << shift;
    printf("%d shifted left by %d positions is %d\n", num, shift, result);

    return 0;
}
You can also try this code with Online C Compiler
Run Code


In this code: 


1. We declare three integer variables: `num` to store the user's input value, `shift` to store the number of positions to shift left, and `result` to store the result of the left shift operation.
 

2. We prompt the user to enter an integer value using `printf()` and read the input using `scanf()`. The input is stored in the `num` variable.
 

3. Similarly, we prompt the user to enter the number of positions to shift left and read the input using `scanf()`. The input is stored in the `shift` variable.
 

4. We perform the left shift operation using the `<<` operator. The value of `num` is shifted left by `shift` positions, and the result is stored in the `result` variable.
 

5. Finally, we display the original value, the number of positions shifted, and the result of the left shift operation using `printf()`.


Let's say the user enters the following inputs:

Enter an integer value: 12
Enter the number of positions to shift left: 2


The output of the program will be:

12 shifted left by 2 positions is 48


In this example, the binary representation of 12 is (0000...1100). When we shift the bits to the left by 2 positions, it becomes (0000...110000), which is equivalent to 48 in decimal.

What Is The Use of Left Shift Operator in C?

1. Multiplying by Powers of 2: One of the most common uses of the left shift operator is to multiply an integer by powers of 2. When you shift the bits of an integer to the left by n positions, it effectively multiplies the number by 2^n. For example, instead of multiplying a number by 4, you can shift its bits to the left by 2 positions. This is often used as an optimization technique because bitwise operations are generally faster than multiplication operations.
 

2. Bit Masking: The left shift operator is used in combination with bitwise AND (&) operator to create bit masks. A bit mask is a binary pattern that is used to extract specific bits from an integer value. By shifting 1 to the left by a desired number of positions and then performing a bitwise AND with the original value, you can isolate specific bits and perform operations on them.
 

3. Packing and Unpacking Data: The left shift operator is useful when you need to pack multiple smaller values into a single larger value or unpack a larger value into smaller components. For example, you can use the left shift operator to combine four 8-bit values into a single 32-bit value. Each 8-bit value is shifted to its appropriate position, and then they are combined using bitwise OR (|) operator.
 

4. Setting Individual Bits: The left shift operator can be used to set individual bits in an integer value. By shifting 1 to the left by the desired bit position and then performing a bitwise OR with the original value, you can set a specific bit to 1 without affecting the other bits.

How Left Shift Operator Works in C?

When you apply the left shift operator (<<) to an integer value, it shifts all the bits of that value to the left by a specified number of positions. The vacant positions on the right are filled with zeros.

Let’s understand in a step-by-step manner how the left shift operation works:

1. The left shift operator takes two operands: the integer value to be shifted (operand1) and the number of positions to shift the bits (operand2).
 

2. The bits of operand1 are shifted to the left by the number of positions specified by operand2.
 

3. The leftmost bits that are shifted beyond the size of the integer are discarded.
 

4. The vacant positions on the right are filled with zeros.
 

5. The resulting value after the shift operation is returned.
 

Let's illustrate this with an example. Consider the following left shift operation:

(10 << 3)


In this case, the binary representation of 10 is (0000...1010). When we shift the bits to the left by 3 positions, here's what happens:

Original bits:   0000...1010
Shifted left by 3 positions:
                 0000...1010000


The three leftmost bits (101) are shifted beyond the size of the integer and are discarded. The three vacant positions on the right are filled with zeros.


The resulting binary representation is (0000...1010000), which is equivalent to 80 in decimal.


Note: The left shift operator multiplies the original value by 2 raised to the power of the number of positions shifted. In this example, (10 << 3) is equivalent to (10 * 2^3), which is (10 * 8) = 80.

Examples to Implement Left Shift Operator in C

Now that we understand the left shift operator and how it works, let's look at some practical examples of implementing it in C programs in different situations: 

Example Showing Left Shift Operation Performed on Two Positive Operands

#include <stdio.h>
int main() {
    int num = 5;
    int shift = 2;
    int result = num << shift;
    printf("%d left shifted by %d positions is %d\n", num, shift, result);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

5 left shifted by 2 positions is 20


In this example, we have a positive integer num with a value of 5 and a positive integer shift with a value of 2. We perform the left shift operation (5 << 2), which shifts the bits of 5 (0000...0101) to the left by 2 positions, resulting in (0000...010100). The resulting value is 20.

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

#include <stdio.h>
int main() {
    int num = 12;
    int shift = -3;
    int result = num << shift;
    
    printf("%d left shifted by %d positions is %d\n", num, shift, result);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

12 left shifted by -3 positions is 12


In this example, we have an integer num with a value of 12 and a negative integer shift with a value of -3. When the second operand (shift) is negative, the behavior is undefined according to the C standard. Most compilers will treat the negative shift value as a large positive value, resulting in undefined behavior. In this case, the output may vary depending on the compiler and platform.

Example Showing Scenario When The Value of First Operand Is Negative

#include <stdio.h>
int main() {
    int num = -10;
    int shift = 2;
    int result = num << shift;    
    printf("%d left shifted by %d positions is %d\n", num, shift, result);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

-10 left shifted by 2 positions is -40


In this example, we have a negative integer num with a value of -10 and a positive integer shift with a value of 2. The left shift operation is performed on the bitwise representation of -10, which is the two's complement representation. The resulting value is -40.

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

#include <stdio.h>
int main() {
    int num = 10;
    int shift1 = 0;
    int shift2 = 32;
    int result1 = num << shift1;
    int result2 = num << shift2;    
    printf("%d left shifted by %d positions is %d\n", num, shift1, result1);
    printf("%d left shifted by %d positions is %d\n", num, shift2, result2);
    
    return 0;
}

You can also try this code with Online C Compiler
Run Code


Output:

10 left shifted by 0 positions is 10
10 left shifted by 32 positions is 10


In this example, we have an integer num with a value of 10. We consider two scenarios:
 

1. When the number of positions to be shifted (shift1) is 0, the left shift operation (10 << 0) does not change the value of num. The resulting value is the same as the original value, which is 10.
 

2. When the number of positions to be shifted (shift2) is greater than or equal to the size of the integer (typically 32 bits for int in most systems), the behavior is undefined according to the C standard. In practice, most compilers will perform a modulo operation on the shift value, effectively reducing it to a value within the valid range. In this case, (10 << 32) is equivalent to (10 << 0), resulting in the original value of 10.

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

#include <stdio.h>
int main() {
    unsigned int num = 10;
    int shift = 3;
    unsigned int result = num << shift;    
    printf("%u left shifted by %d positions is %u\n", num, shift, result);
    
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

10 left shifted by 3 positions is 80


In this example, we use an unsigned integer num with a value of 10 and perform a left shift operation by 3 positions. The left shift operator works similarly with unsigned integers as it does with signed integers. The resulting value is 80.

Undefined Result Of Operator

It's important to be aware of certain situations where the result of the left shift operator can be undefined according to the C standard. Let’s look at a few cases to keep in mind:

1. Shifting by a negative number of positions: If the second operand (the number of positions to shift) is negative, the behavior is undefined. Most compilers will treat the negative shift value as a large positive value, leading to undefined behavior.


Example:

   int num = 10;
   int shift = -3;
   int result = num << shift;  // Undefined behavior


2. Shifting by a number of positions greater than or equal to the size of the integer: If the second operand is greater than or equal to the number of bits in the integer type (typically 32 for int), the behavior is undefined. In practice, most compilers perform a modulo operation on the shift value, effectively reducing it to a value within the valid range.

Example:

   int num = 10;
   int shift = 32;
   int result = num << shift;  // Undefined behavior


3. Shifting a negative number: When shifting a negative number, the behavior is implementation-defined. The sign bit may be shifted into the value, or the sign bit may be preserved. It's best to avoid shifting negative numbers unless you are certain about the behavior on your specific platform.

Example:

   int num = -10;
   int shift = 2;
   int result = num << shift;  // Implementation-defined behavior


Important point to remember: It's crucial to be cautious whenever you are using the left shift operator and ensure that the operands are within valid ranges to avoid undefined behavior. Always consider the size of the integer type and the potential impact of shifting negative numbers or shifting by negative or large positive values.

Frequently Asked Questions

What happens when you left shift a number by a negative value?

Left shifting a number by a negative value results in undefined behavior according to the C standard. Most compilers treat the negative shift value as a large positive value, leading to unexpected results.

Can you left shift a negative number?

Yes, you can left-shift a negative number, but the behavior is implementation-defined. Depending on the platform and compiler, the sign bit may be shifted into the value or preserved.

What is the time complexity of the left shift operator?

The left shift operator is a constant-time operation, meaning it takes O(1) time complexity. It is a fast and efficient way to multiply a number by powers of 2.

Conclusion

In this article, we discussed the left shift operator (<<) in C, which shifts the bits of an integer to the left by a specified number of positions. We learned about its syntax, how it works under the hood, and its various applications. We also looked at examples showing its use in different situations and discussed its undefined behavior. The left shift operator is a powerful tool for bit manipulation, optimization, and low-level programming tasks in C.

You can also check out our other blogs on Code360.

Live masterclass