Left Shift as Multiply by 2

Learn how left shifting (<<) a number by N positions multiplies it by 2^N. Understand overflow behavior and when shifts outperform multiplication.

Shift Operations

Detailed Explanation

Left Shift (<<) as Multiplication

Shifting a binary number left by one position is equivalent to multiplying by 2. Shifting left by N positions multiplies by 2^N.

How It Works

  5       = 00000101
  5 << 1  = 00001010  (10, which is 5 * 2)
  5 << 2  = 00010100  (20, which is 5 * 4)
  5 << 3  = 00101000  (40, which is 5 * 8)

Each shift inserts a 0 on the right and moves all bits one position to the left. The mathematical result is multiplication by 2 for each shift position.

General Formula

A << N = A * (2^N)

Overflow and Width

If the shifted value exceeds the bit width, the high bits are lost. In 8-bit:

  200     = 11001000
  200 << 1 = 10010000  (144, not 400!)

This is equivalent to (A * 2^N) mod 2^width. Always check that your values won't overflow before using shift-based multiplication.

Performance

On most CPUs, a shift instruction takes 1 clock cycle regardless of the shift amount, while multiplication may take 3-5 cycles. Modern compilers automatically convert multiplications by powers of 2 into shifts, but in interpreted languages or hot loops, explicit shifts can measurably improve performance.

Creating Powers of 2

1 << N produces 2^N. This is the standard idiom for creating bitmasks:

const bit0 = 1 << 0;  // 1
const bit7 = 1 << 7;  // 128
const bit31 = 1 << 31; // -2147483648 (signed 32-bit)

Use Case

Embedded systems developers use left shifts for fast multiplication in time-critical interrupt service routines. For example, converting an ADC reading to millivolts might use `reading << 2` (multiply by 4) plus `reading` to approximate a multiply by 5, avoiding the overhead of a hardware multiplier on small microcontrollers that lack one.

Try It — Bitwise Calculator

Open full tool