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

Last Updated : 26 Aug, 2026

Shift operators are bitwise operators in C++ that move the bits of an integer to the left or right. They are commonly used for bit manipulation, bit masking, and operations involving powers of two.

  • Move the binary bits of a value by a specified number of positions.
  • Commonly used in low-level programming, embedded systems, and bit manipulation.

Types of Shift Operators in C++

C++ provides two shift operators that move the bits of a value to the left or right by a specified number of positions.

Left Shift (<<) Operators

The left shift operator << moves the bits of the left operand to the left by the number of positions specified by the right operand. 

Syntax

a << b;

where,

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

For non-negative values where the result is representable, shifting a value left by b positions is equivalent to multiplying it by 2^b.

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++
#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
#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:

  • Bit Manipulation: Moves bits to specific positions while working with binary data.
  • Bit Masking: Helps create or modify bit patterns.
  • Power-of-Two Operations: For suitable non-negative values, left shifting can represent multiplication by powers of two.

Right Shift(>>) Operators

The right shift operator >> moves the bits of the left operand to the right by the number of positions specified by the right operand.

Syntax

a >> b;

where,

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

For non-negative integers, shifting a value right by b positions is equivalent to integer division by 2^b.

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++
#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
#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 commonly used for:

  • Bit Extraction: Moves selected bits toward the least significant position.
  • Bit Manipulation: Helps process individual bits and bit fields.
  • Power-of-Two Operations: For non-negative integers, right shifting can represent integer division by powers of two.

Important Points of Shift Operators

1. Negative Shift Values Cause Undefined Behavior

The shift count must not be negative. If the right operand of a shift operator is negative, the behavior is undefined in C++.

For example:

int shift = -5;

// 2 << shift; // Undefined behavior
// 2 >> shift; // Undefined behavior

Therefore, always ensure that the shift count is non-negative.

C++
#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
#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++
#include <iostream>

using namespace std;

int main()
{
    int N = 3;

    // left shift by 65 digits
    cout << "3 << 65" << (3 << 65) << endl;

    return 0;
}
C
#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^b
  • a >
  • > b ≈ a ÷ 2^b
C++
#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
#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;
}

Output
2^5 using pow() function32
2^5 using leftshift32
Comment