Left Shift and Right Shift Operators in C/C++

Last Updated : 23 May, 2026

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,

  • a is the integer value to be shifted.
  • b specifies 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.

Left-Shift-in-c-cpp
C++
// 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
// 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 n positions is equivalent to multiplying it by 2^n and 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,

  • a is the integer value to be shifted.
  • b specifies how many positions to shift the bits.

Example of Right Shift

Let a = 21 -> Binary: 10101

  • a >> 1 shifts bits right by 1 position
  • Result becomes 1010 -> Decimal: 10
  • Equivalent to: 21 ÷ 2¹ = 10
right-Shift-in-c-cpp
C++
// 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
// 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 n positions is equivalent to dividing it by 2^n and 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++
// 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
// 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 << 33 is invalid

To handle large shifts safely, use:

  • 1ULL (Unsigned Long Long, typically 64-bit)
C++
//    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
//    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 << ba × 2^b
  • a >
  • > ba ÷ 2^b
C++
// 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
// 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++

Comment