Course contentsShow
← Java
Lesson 119 of 2,8703. Operators and ExpressionsFree lesson

Left Shift Operator (<<)

How << shifts bits left, filling with zeros, effectively multiplying by powers of 2.

Left Shift Operator (<<)

You'll learn how the left shift operator moves bits to the left and effectively multiplies numbers by powers of 2.

What Is the Left Shift Operator?

The left shift operator (<<) takes a number's binary representation and shifts all its bits to the left by a specified number of positions. When bits move left, zeros are filled in from the right side. Each shift left is equivalent to multiplying by 2.

Think of it like a conveyor belt moving items to the left: as bits slide left, empty spaces on the right get filled with zeros.

How It Works

The syntax is: value << numberOfPositions

When you write 5 << 2, you're shifting the binary representation of 5 two positions to the left:

  • 5 in binary: 0000 0101
  • After << 2: 0001 0100 (which is 20 in decimal)

Notice what happened mathematically: 5 × 2² = 5 × 4 = 20

Each left shift multiplies by 2:

  • 8 << 1 shifts left once: 8 × 2 = 16
  • 8 << 2 shifts left twice: 8 × 4 = 32
  • 8 << 3 shifts left three times: 8 × 8 = 64

Why Use It?

Left shift is much faster than multiplication when you need to multiply by powers of 2. It's commonly used in low-level programming, graphics, cryptography, and performance-critical code where every operation counts.

Important Notes

The bits that "fall off" the left side are discarded. With large shifts, you can cause overflow just like regular multiplication, resulting in wraparound behavior with integer types.

Key Takeaway: The left shift operator (<<) moves bits left and fills with zeros, effectively multiplying by powers of 2—it's a fast alternative to multiplication for specific cases.