C++ Program to perform Left Shift Operation [DEVCPP/GCC]


LEFT SHIFT OPERATOR  (<<)

The left shift operator shifts the data in left direction by a specified number of bits. New bits coming in from the right are zeros.
Shifting left by n places is the same as multiplying by 2^n. Moreover, Shifting is faster than multiplication.

For instance, i = j << 2 i.e. Multiply j by 4 (2^2)

PROGRAM

//Program to perform Left Shift Operation

#include <iostream>

using namespace std;

int main()
{
int x,res;

cout<<"ENTER X: ";
cin>>x;

res = x<<1; //res=x*(2^1)
cout<<x<<" *  2= "<<res<<endl ;

res = x<<2; //res=x*(2^2)
cout<<x<<" *  4= "<<res<<endl ;

res = x<<3; //res=x*(2^3)
cout<<x<<" *  8= "<<res<<endl ;

res = x<<4; //res=x*(2^4)
cout<<x<<" * 16= "<<res<<endl ;

return 0;
}

OUTPUT

C++ Program to perform Left Shift Operation

Share this

Related Posts

FIND US ON FACEBOOK!