Module 3 · Logic and bit operations · Lesson 5 of 6
Shifting bits left and right
The last two bitwise operators don't combine numbers — they slide the bits of one. << shifts every bit to the left; >> shifts them right. The number after the operator says how many places.
Each left shift doubles, each right shift halves (dropping any remainder) — and it's far faster than multiplying.
Here's the neat part. In binary, moving every bit one place left doubles the value — the same way moving a decimal digit left multiplies by ten. So x << 1 is x × 2, and x << 3 is x × 8 (two to the power of the shift).
Shifting right does the reverse: each place halves the value, dropping any bit that falls off the end — which is why 7 >> 1 gives 3, not 3.5.
Shifts are a fast, classic way to multiply or divide by powers of two, and they pair naturally with masks (1 << n) to reach any bit you like. That's the whole bitwise toolkit — a quick quiz to finish.