How does Shift Operators Work in C++

Hey guys, today we will be learning about shift operators and how they work in C++.

What is a Shift Operator?

Shifting is of 2 types: Left shift and Right shift.
In the Left shift, we are shifting the binary numbers ‘x’ positions to the left where x is a user-entered value.
And in the Right shift, we are shifting the binary numbers ‘x’ positions to the right where again, x is a user-entered value.

Left shift is denoted by <<
Right shift is denoted by >>

For better understanding, take a look at this example,

Assume the user enters the number 5 and tells us it wants us to move 1 position left.
Binary of 5 = 00101.
Now what the left shift operator does is, shift every element 1 position left and add zeros at the end.
So since its 1 position left,
After Shifting = 01010 Which is not 5 anymore.
01010 is the binary form of 10.
And since programs and data are stored in binary, the compiler can easily print it.

For Clear Understanding:

Number = 5
Binary=00101
Move every bit 1 position left and add 0 at the right
=> 001010
Since 5 bits, use the 5 bits from right
=> 01010
=> 10

Syntax:

<variable> << <positions>;

On the other hand, assume the user enters number 5 and says move it 1 position to the right.
Binary of 5 = 00101.
Now what the shift operator does is, shift every binary number 1 position to the right and add 0 to the extreme left.
Therefore after shifting = 00010 Which is not 5
00010 is the binary form of 2.

For Clear Understanding:

Number = 5
Binary=00101
Move every bit 1 position right and add 0 at the left
=> 0001010
Since 5 bits, use the 5 bits from left
=> 00010
=> 2

Syntax:

<variable> >> <positions>

Code Sample of Left Shift Operator in C++:

Let us take a look at the following code:

#include <iostream>
using namespace std;
int main()
{
    int a,lpos,ls;
    cout<<"Enter a number\n";
    cin>>a;
    cout<<"Enter the positions you want to left shift\n";
    cin>>lpos;
    ls=a<<lpos;
    cout<<"After left shifting="<<ls<<"\n";
}

Now let us have a look at the code line by line:

  1. int a,lpos,ls;

    Here I am defining 3 variables of type int.
    a for storing the number.
    lpos for storing how many positions the user wants to shift left.
    ls for storing the shifted number.

  2. cout<<"Enter a number\n"; 
    cin>>a; 
    cout<<"Enter the positions you want to left shift\n"; 
    cin>>lpos;

    Now here we are asking the user to enter the number and then storing it in a
    Then we are asking the user to enter the left shift positions and storing it in lpos.

  3. ls=a<<lpos;

    Here we are performing the left shift and storing the result in ls

  4. cout<<"After left shifting="<<ls<<"\n";

    Finally, we are printing the left-shifted number.

Now we saw an example above of the number 5 and its left shift being 10.
I tried it on this code and the output was:

Enter a number  
5
Enter the positions you want to left shift  
1                                     
After left shifting=10

Code Example of Right Shift Operator in C++:

Let us take a look at the following code:

#include <iostream>
using namespace std;
int main()
{
    int a,rpos,rs;
    cout<<"Enter a number\n";
    cin>>a;
    cout<<"Enter the positions you want to right shift\n";
    cin>>rpos;
    rs=a>>rpos;
    cout<<"After right shifting="<<rs<<"\n";
}

Now let us have a look at the code line by line:

  1. int a,rpos,rs;

    Here I am defining 3 variables of type int.
    a for storing the number.
    rpos for storing how many positions the user wants to shift right.
    rs for storing the shifted number.

  2. cout<<"Enter a number\n"; 
    cin>>a; 
    cout<<"Enter the positions you want to right shift\n"; 
    cin>>rpos;

    Now here we are asking the user to enter the number and then storing it in a
    Then we are asking the user to enter the right shift positions and storing it in rpos.

  3. rs=a>>rpos;

    Here we are performing the right shift and storing the result in rs

  4. cout<<"After right shifting="<<rs<<"\n";

    Finally, we are printing the right-shifted number.

Now we saw an example above of the number 5 and its right shift being 2.
I tried it on this code and the output was:

Enter a number
5 
Enter the positions you want to right shift
1
After right shifting=2

Important Points:

  1. Shifting does not work correctly on negative numbers
  2. If you are using 32 bits, you cannot move more than 32 positions. Similarly, for 64, you cannot move more than 64 positions.

Hence we have covered Lef Shift and Right shift Operators in C++.
Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *