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.
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.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.