Math Fundamentals18 sections · 814 units
Open in Course

Left Shift (<<)

Multiply by powers of 2

The left shift operator (<<) moves all bits to the left by a specified number of positions. New bits on the right are filled with 0.

Example: 5<<25 << 2. In binary, 5=1015 = 101. Shifting left by 2 positions gives 10100=2010100 = 20. Notice that shifting left by kk positions multiplies the number by 2k2^k. Here, 5×22=205 \times 2^2 = 20.

Left shift is how you quickly compute powers of 2. The expression 1<<k1 << k gives you 2k2^k. Want 2102^{10}? Write 1<<101 << 10. It's faster than calling a power function.