Quiz Entry - updated: 2026.07.14
How can you use shift operations for multiplication and division by powers of 2?
Shifting left by k multiplies by 2^k; shifting right by k divides by 2^k. Compilers use this because shifts are faster than multiply/divide.
| Operation | Shift | Example |
|---|---|---|
| × 2 | << 1 |
x << 1 = x × 2 |
| × 4 | << 2 |
x << 2 = x × 4 |
| × 8 | << 3 |
x << 3 = x × 8 |
| ÷ 2 | >> 1 |
x >> 1 = x ÷ 2 |
| ÷ 4 | >> 2 |
x >> 2 = x ÷ 4 |
For non-power-of-2:
x * 14 = x * (16 - 2) = (x << 4) - (x << 1)
x * 14 = x * (8 + 4 + 2) = (x << 3) + (x << 2) + (x << 1)
Why use shifts: Much faster than multiplication on most processors.