Shift operators are a type of bitwise operator in C/C++ that work directly on the binary representation of numbers. They are used to efficiently manipulate data at the bit level, enabling faster computations compared to standard arithmetic operations.
- Operate directly on individual binary bits to perform low-level data manipulation.
- Enable faster arithmetic operations such as multiplication and division by powers of two.
- Widely used in system programming, embedded systems, and performance-critical applications.
Left Shift (<<) Operators
The left shift(<<) is a binary operator that takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, left-shifting an integer "a" with an integer "b" denoted as '(a<<b)' is equivalent to multiplying a with 2^b (2 raised to power b).
Syntax
a << b;
where,
ais the integer value to be shifted.bspecifies how many positions to shift the bits.
Example of Left Shift
Let
a = 21-> Binary:10101
- a << 1 shifts bits left by 1 position
- Result becomes 101010 -> Decimal: 42
- Equivalent to: 21 × 2¹ = 42
If the data type has a fixed bit size (e.g., 5-bit), overflow may discard leftmost bits.

// C++ Program to demonstrate use
// of left shift operator
#include <iostream>
using namespace std;
int main() {
// a = 21(00010101)
unsigned char a = 21;
// The result is 00101010
cout << "a << 1 = " << (a << 1);
return 0;
}
// C Program to demonstrate use
// of left shift operator
#include <stdio.h>
int main() {
// a = 21(000010101)
unsigned char a = 21;
// The result is 00101010
printf("a << 1 = %d\n", (a << 1));
return 0;
}
Output
a << 1 = 42
Applications of Left Shift
Left shift is commonly used for:
- Multiplication by Powers of Two: Left shifting a number by
npositions is equivalent to multiplying it by2^nand is much faster than normal multiplication - Efficient Calculations: Used in performance-critical applications where arithmetic operations need to be fast.
- Bit Manipulation: Common in low-level programming, such as embedded systems and hardware interfacing.
Right Shift(>>) Operators
Right Shift(>>) is a binary operator that takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift. In other words, right-shifting an integer "a" with an integer "b" denoted as '(a>>b)' is equivalent to dividing a with 2^b.
Syntax
a >> b;
where,
ais the integer value to be shifted.bspecifies how many positions to shift the bits.
Example of Right Shift
Let a = 21 -> Binary: 10101
a >> 1shifts bits right by 1 position- Result becomes
1010-> Decimal:10 - Equivalent to:
21 ÷ 2¹ = 10

// C++ Program to demonstrate
// use of right-shift operator
#include <iostream>
using namespace std;
int main() {
// a = 21(00010101)
unsigned char a = 21;
// The result is 00001010
cout << "a >> 1 = " << (a >> 1);
return 0;
}
// C Program to demonstrate
// use of right-shift operator
#include <stdio.h>
// Driver code
int main()
{
// a = 21(00010101)
unsigned char a = 21;
// The result is 00001010
printf("a >> 1 = %d\n", (a >> 1));
return 0;
}
Output
a >> 1 = 10
Applications of Right Shift
Right shift is used for:
- Division by Powers of Two: Right shifting a number by
npositions is equivalent to dividing it by2^nand it is very fast. - Efficient Calculations: Used in performance-critical applications for fast division operations.
- Bit Manipulation: Useful in extracting specific bits from data, common in data compression and cryptography.
Important Points of Shift Operators
1. Negative Shift Values Cause Undefined Behavior
The left-shift (<<) and right-shift (>>) operators must not use negative shift counts. If the shift value is negative, the behavior is undefined in C++, meaning the result is not predictable.
// C++ program to show behaviour of shift operators for
// negative values
#include <iostream>
using namespace std;
int main()
{
// left shift for negative value
cout << "2 << -5 = " << (2 << -5) << endl;
// right shift for negative value
cout << "2 >> -5 = " << (2 >> -5) << endl;
return 0;
}
// C program to show behaviour of shift operators for
// negative values
#include <stdio.h>
int main()
{
// left shift for negative value
printf("2 << -5 = %d\n", (2 << -5));
// right shift for negative value
printf("2 >> -5 = %d", (2 >> -5));
return 0;
}
Output:
- Not fixed or reliable
- May differ across compilers
- Should NOT be assumed as 0, 64, etc.
2. Overshifting Beyond Bit Size is Undefined
If a number is shifted beyond the size of its data type, the result is undefined behavior.
Example:
- In a 32-bit integer, shifting like
1 << 33is invalid
To handle large shifts safely, use:
1ULL(Unsigned Long Long, typically 64-bit)
// c++ program to demonstrate the behaviour of bitwise
// shift operators for large values
#include <iostream>
using namespace std;
int main()
{
int N = 3;
// left shift by 65 digits
cout << "3 << 65" << (3 << 65) << endl;
return 0;
}
// c program to demonstrate the behaviour of bitwise
// shift operators for large values
#include <stdio.h>
int main()
{
int N = 3;
// left shift of 65 digits
printf("3 << 65 = %d", (3 << 65));
return 0;
}
Output:
- Not defined
- May produce garbage or unexpected results
- Cannot be relied upon
3. Shift Operators and Powers of Two
Shift operators are closely related to multiplication and division by powers of two:
a << b≈a × 2^ba >> b≈a ÷ 2^b
// C++ program to get the shifted values using pow()
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << "2^5 using pow() function" << pow(2, 5) << endl;
cout << "2^5 using leftshift" << (1 << 5) << endl;
return 0;
}
// C program for the above approach
#include <math.h>
#include <stdio.h>
int main()
{
printf("2^5 using pow() function: %.0f\n", pow(2, 5));
printf("2^5 using left shift: %d\n", (1 << 5));
return 0;
}
// This code is contributed Prince Kumar
Output
2^5 using pow() function32 2^5 using leftshift32
Related Article: Bitwise Operators in C/C++