Left shift operator

Left shift operator?

The left shift operator (<<) is a binary operator. It is primarily used to perform a bitwise left shift operation on the binary representation of a number.In simple terms, the left shift operator shifts the bits of a binary number to the left by a specified number of positions. For each shift left by n positions, the number is effectively multiplied by 2^n.

Left shift operator example

const num = 5; // Binary representation: 0000 0101
const shiftedNum = num << 2; // Shift left by 2 positions
// After shifting left by 2 positions: 0001 0100
// Decimal value of shiftedNum = 20

Useful properties left shift operator:

Left shifting a number by n positions is equivalent to multiplying the number by 2^n.
If the left shift causes any bits to be shifted out (loses the leftmost bits), those bits are discarded.
Shifting a number left by n positions is the same as multiplying the number by 2^n for positive numbers and shifting towards higher significant bits for negative numbers in two's complement representation.