Skip to content

Bitwise Left Shifts

The left-shift operator causes the bits in shift-expression to be shifted to the left by the number of positions specified by additive-expression. The bit positions that have been vacated by the shift operation are zero-filled.

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

Introduction

The left-shift operator causes the bits in shift-expression to be shifted to the left by the number of positions specified by an additive expression. The bit positions that the shift operation has vacated are zero-filled.

Left shift

The left shift operator is written as <<.

Integers are stored in memory as a series of bits. For example, the number 6 stored as a 32-bit int would be:

6 = 00000000 00000000 00000000 00000110

Shifting this bit pattern to the left one position (6 << 1) would result in the number 12:

 6 << 1 = 00000000 00000000 00000000 00001100

As you can see, the digits have shifted to the left by one position, and the last digit on the right is filled with a zero. You might also note that shifting left is equivalent to multiplication by powers of 2.

So,

6 << 1 → 6 * 2^1 → 6 * 2

6 << 3 → 6 * 2^3 → 6 * 8

A good optimization compiler will replace multiplications with shifts when possible.

32-bit representation

00000000 00000000 00000000 00000010 << 1  →  00000000 00000000 00000000 00000100

4-bit representation

0010 << 2  →  1000

As seen above, a single left shift multiplies a binary number by 2.

0010 << 1  →  0100

0010 is 2
0100 is 4

So, in simple terms, << (left shift) takes two numbers, left shifts the bits of the first operand, and the second operand decides the number of places to shift.

Let’s see how to represent this in a mathematical formula.

Formula

a << b = (a * 2b)

Solution

Java

class LeftShift {
    private static int helper(int number, int i) {
        return number << i;// multiplies `number` with 2^i times.
    }

    public static void main(String[] args) {
        int number = 100;

        System.out.println(number + " shifted 1 position left, yields to " + helper(number, 1));
        System.out.println(number + " shifted 2 positions left, yields to " + helper(number, 2));
        System.out.println(number + " shifted 3 positions left, yields to " + helper(number, 3));
        System.out.println(number + " shifted 4 positions left, yields to " + helper(number, 4));
    }
}

Python

def helper(number,i):
    return number << i

number=100
print(number , "shifted 1 position left,yields to ",helper(number,1))
print(number , "shifted 2 position left,yields to ",helper(number,2))
print(number , "shifted 3 position left,yields to ",helper(number,3))
print(number , "shifted 4 position left,yields to ",helper(number,4))

JavaScript

const LeftShift = (number, i) => number << i;

let number = 100;

console.log (number + " shifted 1 position left, yields to " + LeftShift (number, 1));
console.log (number + " shifted 2 positions left, yields to " + LeftShift (number, 2));
console.log (number + " shifted 3 positions left, yields to " + LeftShift (number, 3));
console.log (number + " shifted 4 positions left, yields to " + LeftShift (number, 4));

C++

#include <iostream>
using namespace std;

int leftShift(int number, int i){
  return number << i;
}

int main() {
  int number = 100;
  cout << number << " shifted 1 position left, yields to " << leftShift (number, 1) << endl;
  cout << number << " shifted 2 position left, yields to " << leftShift (number, 2) << endl;
  cout << number << " shifted 3 position left, yields to " << leftShift (number, 3) << endl;
  cout << number << " shifted 4 position left, yields to " << leftShift (number, 4) << endl;
  return 0;
}

TypeScript

export const LeftShift = (n: number, i: number): number => n << i;

let n: number = 100;

console.log(n + " shifted 1 position left, yields to " + LeftShift(n, 1));
console.log(n + " shifted 2 positions left, yields to " + LeftShift(n, 2));
console.log(n + " shifted 3 positions left, yields to " + LeftShift(n, 3));
console.log(n + " shifted 4 positions left, yields to " + LeftShift(n, 4));

So, shifting to the left is equivalent to multiplication by powers of 2.

Let’s see some left-shift operator examples in the next chapter.

Bit ManipulationData Structures and Algorithms

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things