Table of contents
1.
Introduction
2.
Left Shift (<<) Operator
2.1.
Syntax
2.2.
Example of Left Shift Operator
2.3.
Applications of Left Shift Operator
3.
Right Shift (>>) Operator
3.1.
Syntax
3.2.
Example of Right Shift Operator:
3.3.
Applications of Right Shift Operator
4.
Important Points of Shift Operators
5.
Frequently Asked Questions 
5.1.
Can shift operators be used with floating-point numbers?
5.2.
What happens when you left shift a number by a negative number of positions?
5.3.
Are shift operators faster than multiplication and division?
6.
Conclusion
Last Updated: Dec 2, 2024
Easy

Shift Operator in C

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

Introduction

Operators in programming languages are special symbols or phrases that tell the compiler or interpreter to perform specific mathematical, relational, or logical operations and return a result. They are similar to verbs in the English language. Just as verbs express action or a state of being—like "run," "is," or "have"—operators in a programming language trigger actions on data. Among these operators, the shift operators in C allow you to shift bits left or right within an integer value. These operators provide a quick way to multiply or divide numbers by powers of two. 

Shift Operator in C

In this article, we will discuss the left shift (<<) & right shift (>>) operators in C, their syntax, examples, and applications. 

Left Shift (<<) Operator

The left shift operator (<<) in C shifts the bits of a number to the left by a specified number of positions. When you left shift a number, the bits are moved to the left, and the rightmost bits are filled with zeros. Each left shift essentially doubles the number, which is equivalent to multiplying the number by 2 raised to the power of the number of positions shifted.

Syntax

The syntax for the left shift operator in C is :

number << positions


Here, "number" represents the integer value you want to shift, and "positions" specifies the number of positions to shift the bits to the left.

Example of Left Shift Operator

Let's take a look at an example to understand how the left shift operator works in C:

#include <stdio.h>

int main() {
    int num = 5;
    int shifted = num << 2;
    printf("Original number: %d\n", num);
    printf("Shifted number: %d\n", shifted);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Original number: 5
Shifted number: 20


In this example, we have an integer variable `num` with a value of 5. We apply the left shift operator (`<<`) to `num` and shift it by 2 positions. The result is stored in the `shifted` variable.

The binary representation of 5 is `0101`. When we left shift it by 2 positions, it becomes `010100`, which is equivalent to the decimal value 20.

Therefore, left shifting 5 by 2 positions is equivalent to multiplying 5 by 2^2 (2 raised to the power of 2), which equals 20.

Applications of Left Shift Operator

The left shift operator has several applications in C language like:

1. Multiplying by powers of 2: Left shifting a number by `n` positions is equivalent to multiplying it by 2^n. This can be a faster alternative to multiplication when the multiplier is a power of 2.
 

2. Bitwise manipulation: Left shift is often used in low-level programming & embedded systems to manipulate individual bits of a number. It can be used to set, clear, or toggle specific bits.
 

3. Packing data: Left shift can be used to pack multiple smaller values into a single larger value. For example, you can use left shift to combine two 16-bit integers into a single 32-bit integer.
 

4. Encryption & compression: Left shift is sometimes used in encryption algorithms & data compression techniques to rearrange or transform data.

Right Shift (>>) Operator

The right shift operator (>>) in C shifts the bits of a number to the right by a specified number of positions. When you right-shift a number, the bits are moved to the right, and the leftmost bits are filled based on the sign of the number. For unsigned numbers, the leftmost bits are filled with zeros. For signed numbers, the leftmost bits are filled with the sign bit (0 for positive numbers and 1 for negative numbers). Each right shift effectively divides the number by 2, discarding any remainder.

Syntax

The syntax for the right shift operator in C is :

number >> positions


Here, "number" represents the integer value you want to shift, and "positions" specifies the number of positions to shift the bits to the right.

Example of Right Shift Operator:

Let's discuss an example to understand how the right shift operator works in C:

#include <stdio.h>
int main() {
    int num = 20;
    int shifted = num >> 2;
    printf("Original number: %d\n", num);
    printf("Shifted number: %d\n", shifted);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Original number: 20
Shifted number: 5


In this example, we have an integer variable `num` with a value of 20. We apply the right shift operator (`>>`) to `num` and shift it by 2 positions. The result is stored in the `shifted` variable.

The binary representation of 20 is `10100`. When we right shift it by 2 positions, it becomes `00101`, which is equivalent to the decimal value 5.

Therefore, right shifting 20 by 2 positions is equivalent to dividing 20 by 2^2 (2 raised to the power of 2), which equals 5.

Applications of Right Shift Operator

1. Dividing by powers of 2: Right shifting a number by `n` positions is equivalent to dividing it by 2^n. This can be a faster alternative to division when the divisor is a power of 2.
 

2. Bitwise manipulation: Right shift is often used in low-level programming & embedded systems to manipulate individual bits of a number. It can be used to extract specific bits or to perform certain bitwise operations.

 

3. Unpacking data: Right shift can be used to unpack values that were previously packed using left shift. By right shifting, you can extract the original smaller values from a larger packed value.
 

4. Optimization techniques: Right shift is sometimes used in optimization techniques to divide numbers efficiently or to perform certain mathematical operations.

Important Points of Shift Operators

1. Shift operators are binary operators, meaning they operate on two operands: the number to be shifted and the number of positions to shift.


2. The left operand of the shift operator must be an integer type (char, short, int, long, or their unsigned versions). The right operand must be a non-negative integer.


3. Shifting a number by a negative number of positions or by a number greater than or equal to the number of bits in the left operand leads to undefined behavior.


4. When left shifting, the rightmost bits are filled with zeros. When right-shifting unsigned numbers, the leftmost bits are filled with zeros. When right-shifting signed numbers, the leftmost bits are filled with the sign bit.


5. Shifting a number by 0 positions results in the original number.


6. Shift operators have lower precedence than arithmetic operators (+, -, *, /) but higher precedence than relational (<, >, <=, >=) and equality (==, !=) operators.


7. It's important to be cautious when using shift operators with signed numbers, as the behavior of right shift on negative numbers is implementation-defined and can vary across compilers and platforms.

Frequently Asked Questions 

Can shift operators be used with floating-point numbers?

No, shift operators can only be used with integer types in C.

What happens when you left shift a number by a negative number of positions?

Shifting a number by a negative number of positions leads to undefined behavior in C.

Are shift operators faster than multiplication and division?

Shift operators can be faster than multiplication and division when the multiplier or divisor is a power of 2.

Conclusion

In this article, we discussed the shift operators in C: left shift (<<) & right shift (>>). We learned about their syntax, examples, and applications. Shift operators provide an efficient way to multiply or divide integers by powers of 2 and are useful for bitwise manipulation, packing and unpacking data, and optimization techniques. 

You can also check out our other blogs on Code360.

Live masterclass