Bit Shifting

A bit shift moves each digit in a number's binary representation left or right. The last bit in the direction of the shift is lost, and a 0 bit is inserted on the other end.

0010 << 1 → 0100 1011 >> 1 → 0101

Bit shifts take number of times to shift as the right argument:

1010110 << 2 → 1011000 1011010 >> 3 → 0001011

A single left shift multiplies a binary number by 2:

0010 << 1 → 0100 0010 is 2 0100 is 4

Two left shifts multiplies by 4. Three left shifts multiplies by 8.

And similarly, shifting right divides by 2, throwing out any remainders.

See also:

Pass Your Interviews with My FREE 7-Day Crash Course

I'll teach you the right way of thinking for breaking down tricky algorithmic coding interview questions you've never seen before.

No prior computer science training necessary—I'll get you up to speed quickly, skipping all the overly academic stuff.

No spam. One-click unsubscribe if you hate it.

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

Psst. Pass it on.

. . .