Left Shift Operator in C++

Introduction

The left shift bitwise operator () takes two numbers and shifts the first operand's bits to the left by the amount indicated by the second operand. For instance, the expression (xy) can be used to left-shift the bits of x by y places. It is the same as dividing x by 2^y.

The following example illustrates how a left shift operator operates:

1000 << 2 returns 4000

(In Binary)

1000         ->     1111101000

<< 2                     |  left shift the bits

-----                      V  by 2 places

4000         <-      111110100000

(In Binary)

Code

#include <iostream>
using namespace std;

int main (){
  int x = 1000;
  int z;

  //left shift bitwise

  z = x << 2;
  cout<<"z = "<<z;
  return 0;
}

Output:

z = 4000

write your code here: Coding Playground

Example: Count number of 1 Bits in a positive integer

Think of the number 1000. It can be expressed in bit-wise format as 1111101000. Not all bits, though, are written here. A 32-bit representation that is comprehensive is as follows:

00000000000000000000001111101000

When performing a bitwise AND with a 1 at any bit, the result is 1 if a bit is 1 or 0 if it is 0. The tally of 1 bits in the provided positive integer is obtained by performing such an operation on each bit and determining the number of 1s. The following example demonstrates how to use the left shift operator to achieve this goal:

#include <iostream>
using namespace std;

int CountOneBits(int n) {
  int mask = 1;
  int count = 0;
 
  //performing bitwise AND operation at every bit of the number
  for(int i = 0; i < 32; ++i) {
    if((mask & n) == mask)
      count++;
    mask = mask << 1;
  }
  return count;
}

int main() {
  cout<<"CountOneBits(1000) = "<<
    CountOneBits(1000)<<"\n";
  cout<<"CountOneBits(1023) = "<<
    CountOneBits(1023)<<"\n";  
  return 0;
}



Output:


CountOneBits(1000) = 6

CountOneBits(1023) = 10

As you can see, there is a zero in the final digit on the right and the digits have moved one position to the left. You should also be aware that moving left is the same as multiplying by two-digit powers of two. The equivalents of 6 * 2 and 6 * 8 are 6 * 1 and 6 * 3, respectively. When possible, a good optimizing compiler will switch out multiplications for shifts.

Things to Remember

  • When dealing with negative numbers, you shouldn't use the left-shift or right-shift operators. If either of the inputs is a negative number, the behavior is undefinable. Answers of either 1 >> -1 and 1 -1, for instance, are undefined.
  • The behavior is unknown if the number is moved by an amount greater than the size of an integer. For instance, if numbers are stored using 32 bits, 1 33 is undefined. Unsigned Long Long, which is defined using 64 bits and can hold huge quantities, 1ULL62 ULL, is used for bit shifts of larger values.
  • The division of the first term and the second term raised to the power 2 and the left-shift by one are identical to the first term and the second term raised to the power 3 and the right-shift by one, respectively (1>>3 = 1/pow(2,3)).