Open In App

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

Last Updated : 04 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Left Shift(<<)

It 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;
  • a: First Operand
  • b: Second Operand

Example: Let’s take a=5; which is 101 in Binary Form. Now, if “a is left-shifted by 2” i.e a=a<<2 then a will become a=a*(2^2). Thus, a=5*(2^2)=20 which can be written as 10100.

left shift operator function

 


C




// C Program to demonstrate use
// of left shift  operator
#include <stdio.h>
 
// Driver code
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", (b << 1));
    return 0;
}


C++




// C++ Program to demonstrate use
// of left shift  operator
#include <iostream>
using namespace std;
 
// Driver code
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;
}


Output

a<<1 = 10
b<<1 = 18

Right Shift(>>)

It 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;
  • a: First Operand
  • b: Second Operand

Example: let’s take a=5; which is 101 in Binary Form. Now, if “a is right-shifted by 2i.e a=a>>2 then a will become a=a/(2^2). Thus, a=a/(2^2)=1 which can be written as 01.

right shift operator function

 


C




// C Program to demonstrate
// use of right-shift operator
#include <stdio.h>
 
// Driver code
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", (b >> 1));
 
    return 0;
}


C++




// C++ Program to demonstrate
// use of right-shift operator
#include <iostream>
using namespace std;
 
// Driver code
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;
}


Output

a>>1 = 2
b>>1 = 4

Important Points

1. The left-shift and right-shift operators should not be used for negative numbers. The result of is undefined behavior if any of the operands is a negative number. For example results of both 1 >> -1 and 1 << -1 is undefined.

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;
}


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;
}


Output

2 << -5 = 0
2 >> -5 = 64

2. If the number is shifted more than the size of the integer, the behavior 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 that can store large values.

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;
}


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;
}


Output

3 << 65 = 0

3. The left-shift by 1 and right-shift by 1 are equivalent to the product of the first term and 2 to the power given element(1<<3 = 1*pow(2,3)) and division of the first term and second term raised to power 2 (1>>3 = 1/pow(2,3)) respectively. 

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


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;
}


Output

2^5 using pow() function: 32
2^5 using left shift: 32

Must Read: Bitwise Operators in C/C++



Similar Reads

Split a Binary String such that count of 0s and 1s in left and right substrings is maximum
Given a binary string, str of length N, the task is to find the maximum sum of the count of 0s on the left substring and count of 1s on the right substring possible by splitting the binary string into two non-empty substrings. Examples: Input: str = "000111" Output: 6 Explanation: Splitting the binary string into "000" and "111". Count of 0s in the
7 min read
C++ Program to Minimize characters to be changed to make the left and right rotation of a string same
Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = “abcd”Output: 2Explanation:String after the left shift: “bcda”String after the right shift: “dabc”Changing the character at position 3 to 'a' and c
3 min read
C++ Program for Longest subsequence of a number having same left and right rotation
Given a numeric string S, the task is to find the maximum length of a subsequence having its left rotation equal to its right rotation. Examples: Input: S = "100210601" Output: 4 Explanation: The subsequence "0000" satisfies the necessary condition. The subsequence "1010" generates the string "0101" on left rotation and string "0101" on right rotat
4 min read
C++ Program to Count of Array elements greater than all elements on its left and at least K elements on its right
Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The only array elements satisfying the given conditions
9 min read
C++ Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
C++ Program for Left Rotation and Right Rotation of a String
Given a string of size n, write functions to perform the following operations on a string- Left (Or anticlockwise) rotate the given string by d elements (where d &lt;= n)Right (Or clockwise) rotate the given string by d elements (where d &lt;= n). Examples: Input : s = "GeeksforGeeks" d = 2 Output : Left Rotation : "eksforGeeksGe" Right Rotation :
5 min read
Print all paths from top left to bottom right in a matrix with four moves allowed
The problem is to print all the possible paths from top left to bottom right of an mXn matrix with the constraints that from each cell you can either move up, right, left or down. Examples: Input : 1 2 3 4 5 6 Output : 1 2 3 6 1 2 5 6 1 4 5 6 4 5 2 3 6 Input : 1 2 3 4 5 6 7 8 9 Output : 1 2 3 6 9 1 2 3 6 5 8 9 1 2 3 6 5 4 7 8 9 1 2 5 6 9 1 2 5 8 9
9 min read
Left-Right traversal of all the levels of Binary tree
Given a Binary Tree rooted at node 1, the task is to print the elements in the following defined order. First, print all elements of the last level in an alternate way such as first you print leftmost element and then rightmost element &amp; continue in this until all elements are traversed of last level.Now do the same for the rest of the levels.E
11 min read
valarray shift() in C++
The shift() function is defined in valarray header file. This function returns a new valarray of the same size with elements whose positions are shifted by n elements. If n is negative, right-shift is applied, if n is positive left-shift is applied. Syntax: valarray shift (int n) const; Parameter: This method accepts a mandatory parameter n which r
2 min read
C++ Program to Modify a string by performing given shift operations
Given a string S containing lowercase English alphabets, and a matrix shift[][] consisting of pairs of the form{direction, amount}, where the direction can be 0 (for left shift) or 1 (for right shift) and the amount is the number of indices by which the string S is required to be shifted. The task is to return the modified string that can be obtain
3 min read
Article Tags :
Practice Tags :