Left Shift and Right Shift Operators in C/C++
Left Shift :
Denoted as : <<
Eg: N<<i (N: first operand, i: second operand)
Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift. Or in other words left shifting an integer “x” with an integer “y” denoted as ‘(x<<y)’ is equivalent to multiplying x with 2^y (2 raised to power y).
eg: lets take N=22; which is 00010110 in Binary Form.
Now, if “N is left-shifted by 2” i.e N=N<<2 then N will become N=N*(2^2). Thus, N=22*(2^2)=88 which can be written as 01011000.
C
/* C++ Program to demonstrate use of left shift operator */ #include<stdio.h> int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00001010 printf ( "a<<1 = %d\n" , a<<1); // The result is 00010010 printf ( "b<<1 = %d\n" , b<<1); return 0; } |
C++
/* C++ Program to demonstrate use of left shift operator */ #include <iostream> using namespace std; int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00001010 cout << "a<<1 = " << (a<<1) << endl; // The result is 00010010 cout << "b<<1 = " << (b<<1) << endl; return 0; } // This code is contributed by shivanisinghss2110 |
a<<1 = 10 b<<1 = 18
Right Shift :
Denoted as : >>
Eg: N>>i (N: first operand, i: second operand)
Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift. In other words right shifting an integer “x” with an integer “y” denoted as ‘(x>>y)‘ is equivalent to dividing x with 2^y.
eg: lets take N=32; which is 100000 in Binary Form.
Now, if “N is right-shifted by 2” i.e N=N>>2 then N will become N=N/(2^2). Thus, N=32/(2^2)=8 which can be written as 1000.
C++
/* C++ Program to demonstrate use of right shift operator */ #include <iostream> using namespace std; int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00000010 cout << "a>>1 = " << (a >> 1)<< endl; // The result is 00000100 cout << "b>>1 = " << (b >> 1) << endl; return 0; } // This code is contributed by shivanisinghss2110 |
C
/* C++ Program to demonstrate use of right shift operator */ #include <stdio.h> using namespace std; int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00000010 printf ( "a>>1 = %d\n" , a >> 1); // The result is 00000100 printf ( "b>>1 = %d\n" , b >> 1); return 0; } |
a>>1 = 2 b>>1 = 4
Important Points :
- The left shift and right shift operators should not be used for negative numbers. The result of is undefined behaviour if any of the operands is a negative number. For example results of both 1 >> -1 and 1 << -1 is undefined.
- If the number is shifted more than the size of integer, the behaviour is undefined. For example, 1 << 33 is undefined if integers are stored using 32 bits. For bit shift of larger values 1ULL<<62 ULL is used for Unsigned Long Long which is defined using 64 bits which can store large values.
- The left-shift by 1 and right-shift by 1 are equivalent to the product of first term and 2 to the power given element(1<<3 = 1*pow(2,3)) and division of first term and second term raised to power 2 (1>>3 = 1/pow(2,3)) respectively.
As mentioned in point 1, it works only if numbers are positive.
C++
#include <iostream> using namespace std; int main() { int x = 19; unsigned long long y = 19; cout << "x << 1 = " << (x << 1) << endl; cout << "x >> 1 = " << (x >> 1) << endl; // shift y by 61 bits left cout << "y << 61 = " << (y << 61) << endl; return 0; } // this code is contributed by shivanisinghss2110 |
C
#include <stdio.h> int main() { int x = 19; unsigned long long y = 19; printf ( "x << 1 = %d\n" , x << 1); printf ( "x >> 1 = %d\n" , x >> 1); // shift y by 61 bits left printf ( "y << 61 = %lld\n" , y << 61); return 0; } |
x << 1 = 38 x >> 1 = 9 y << 61 = 6917529027641081856
- The left-shift of 1 by i is equivalent to 2 raised to power i.
As mentioned in point 1, it works only if numbers are positive.
C++
#include <iostream> using namespace std; int main() { int i = 3; cout << "pow(2, " << i << ") = " << (1 << i) << endl; i = 4; cout << "pow(2, " << i << ") = " << (1 << i) << endl; return 0; } // this code is contributed by shivanisinghss2110 |
C
#include<stdio.h> int main() { int i = 3; printf ( "pow(2, %d) = %d\n" , i, 1 << i); i = 4; printf ( "pow(2, %d) = %d\n" , i, 1 << i); return 0; } |
pow(2, 3) = 8 pow(2, 4) = 16
Interesting Facts about Bitwise Operators in C
Please Login to comment...