What Is the Bit Shift Operator? Left, Right, and Every Interview Pattern

June 18, 20268 min read
dsaalgorithmsinterview-prepleetcode
What Is the Bit Shift Operator? Left, Right, and Every Interview Pattern
TL;DR
  • Left shift (n << k) multiplies by 2ᵏ; right shift (n >> k) floor-divides by 2ᵏ, both O(1) with no loops or allocations
  • Arithmetic vs logical right shift: most languages use arithmetic >> (sign bit preserved for negatives); Java's >>> and JavaScript's >>> give unsigned (logical) results
  • Four mask patterns built on 1 << k: OR to set a bit, AND NOT to clear, XOR to toggle, shift-then-AND-1 to check
  • 1 << n generates 2ⁿ, the exact count of subsets of n items and the direct entry point for bitmask DP state spaces
  • Left shift overflow in 32-bit types silently sets the sign bit and returns a negative number; Python has arbitrary precision but integers can grow surprisingly large

You've written 1 << k in solutions enough times that it feels like muscle memory. Then an interviewer asks you to explain it and your brain quietly plays a loading spinner for three seconds.

The bit shift operator moves every bit in an integer left or right by some number of positions. That's it. The whole mechanism fits in one sentence. What makes it worth understanding is what falls out from it: multiplication and division by powers of two in a single CPU cycle, plus a set of building-block operations that cover the majority of bit manipulation questions you'll ever see.

Binary Is Just Columns

To see what a shift does, you need to see what an integer looks like in memory.

Take the number 13. In 8-bit binary:

bit position:  7  6  5  4  3  2  1  0
value:         0  0  0  0  1  1  0  1

Each position is a power of two: position 0 is 2⁰ = 1, position 1 is 2¹ = 2, position 3 is 2³ = 8. So 13 = 8 + 4 + 1 = 1101 in binary.

A bit shift slides all of those bits left or right by some number of columns. Zeros fill the vacated positions on one side. Bits that fall off the other side are gone, no questions asked.

Left Shift Means Multiply by Two

The left shift operator << moves every bit toward the high-order (leftmost) end.

n = 1 # binary: 00000001 n << 1 # binary: 00000010 → 2 n << 2 # binary: 00000100 → 4 n << 3 # binary: 00001000 → 8

Each left shift by one position is exactly multiplication by 2. Shift by k positions and you get multiplication by 2ᵏ. This is not an approximation or a coincidence. It is the same reason appending a zero to a decimal number multiplies it by ten.

5 << 1 # 10 (5 × 2) 5 << 3 # 40 (5 × 8) 1 << 10 # 1024 (a useful shorthand for 2¹⁰)

Right Shift Means Divide by Two

The right shift operator >> moves every bit toward the low-order (rightmost) end.

n = 40 # binary: 00101000 n >> 1 # binary: 00010100 → 20 n >> 2 # binary: 00001010 → 10 n >> 3 # binary: 00000101 → 5

Right shifting by k positions divides by 2ᵏ, truncating toward negative infinity. For positive integers that is just floor division. The bits that fall off the right are gone, same as before.

20 >> 1 # 10 (20 ÷ 2) 20 >> 2 # 5 (20 ÷ 4) 7 >> 1 # 3 (7 ÷ 2, truncated)

The Catch: Arithmetic vs Logical Right Shift

This is where most engineers discover that they had, in fact, been guessing all along.

When you right shift a positive number, zeros always fill in from the left. No ambiguity. When you right shift a negative number, two valid behaviors exist:

  • Logical right shift: always fills with zeros, treating the value as unsigned.
  • Arithmetic right shift: fills with copies of the sign bit (1 for negative numbers, 0 for positive), which preserves the division semantics for signed integers.

Most languages use arithmetic right shift for >>, which is why right-shifting -8 by 1 gives you -4, not some enormous positive number.

# Python -8 >> 1 # -4 (arithmetic: sign bit preserved)
// Java has both, explicitly int x = -8; x >> 1 // -4 (arithmetic) x >>> 1 // 2147483644 (logical: fills with 0s, treats as unsigned 32-bit)

Java exposes both operators. The >>> is the logical (unsigned) right shift. Python, C, C++, and Go all use arithmetic >> for signed integers. JavaScript has all three: <<, >> (arithmetic), and >>> (logical).

-8 >> 1 // -4 (arithmetic) -8 >>> 1 // 2147483644 (logical, 32-bit)

For positive integers, all of these behave identically. Most interview problems use non-negative inputs, so this distinction rarely bites you in the actual code. It does, however, come up the moment anyone asks "any edge cases?"

Both Operators Are O(1)

Left shift and right shift are both O(1) time, O(1) space. One CPU instruction. No loops, no allocations, no recursion.

The only way to get faster arithmetic is to do no arithmetic at all. Modern compilers optimize constant-power-of-two multiplies to shifts anyway, so in many contexts there is no runtime difference. The point is signal: using n >> 1 instead of n // 2 tells the interviewer you understand what the machine is actually doing underneath.

Four Patterns Cover Every Bit Interview Question

1 << k creates an integer with exactly one bit set at position k. Every bit manipulation problem you see in an interview is built on that single mask. There are four operations and you should memorize them as a group.

Checking whether bit k is set

def is_bit_set(n: int, k: int) -> bool: return (n >> k) & 1 == 1

Shift n right by k, then AND with 1. Result is 0 or 1. Alternatively: (n & (1 << k)) != 0.

Setting bit k

def set_bit(n: int, k: int) -> int: return n | (1 << k)

OR with a mask that has a single 1 at position k. Turns that bit on, leaves everything else alone.

Clearing bit k

def clear_bit(n: int, k: int) -> int: return n & ~(1 << k)

~(1 << k) is all 1s except position k. AND-ing with n zeroes out only that position.

Toggling bit k

def toggle_bit(n: int, k: int) -> int: return n ^ (1 << k)

XOR with the mask. Bit was 1, it becomes 0. Bit was 0, it becomes 1. For more XOR tricks, see XOR in Coding Interviews.

The shift is always 1 << k. What changes is the operation: OR to set, AND NOT to clear, XOR to toggle, AND 1 (after shifting) to check.

Tom the cat dressed impeccably for a technical interview, then destroyed by the actual job

Confidently explaining bit shifts in the interview vs. staring at >>> in production Java at 2am.

Where 1 << k Shows Up Outside of Masks

Two other contexts come up constantly.

Checking whether n is a power of two:

def is_power_of_two(n: int) -> bool: return n > 0 and (n & (n - 1)) == 0

Powers of two have exactly one bit set. n - 1 clears that bit and sets all lower bits. AND-ing them returns 0 if exactly one bit was set. The shift is implicit: you are reasoning about the single-bit structure that 1 << k would produce.

Bitmask DP state spaces:

When a problem asks you to track every subset of n items, you need 2ⁿ states. 1 << n generates that count directly.

n = 4 for mask in range(1 << n): # 0 to 15: all subsets of 4 items for k in range(n): if (mask >> k) & 1: pass # item k is included in this subset

Each integer from 0 to (1 << n) - 1 encodes one possible subset. The bitmask DP pattern lives entirely on this representation. The four patterns above let you check or modify individual elements in O(1).

Two Mistakes That Show Up in Real Code

Left shift overflow in fixed-width types. In Java, C, and C++, integers have fixed width. Shift too far left and bits drop off the edge. 1 << 31 in a 32-bit signed integer sets the sign bit and hands you a negative number. Python uses arbitrary-precision integers, so there is no overflow, but the values can grow surprisingly fast if you are not paying attention. Mention overflow in any left shift involving a fixed-width type when the interviewer asks about edge cases.

Assuming >> gives you an unsigned result. If you right-shift a negative number expecting a small positive value, you will get a negative value in most languages. Use >>> in Java or JavaScript when you need logical (unsigned) behavior. In C and C++, right-shifting a negative signed integer is implementation-defined before C++20. The portable fix is to cast to an unsigned type first.

The Bit Shift Operator Is One Instruction. Explaining It Is the Whole Test.

The real reason interviews test bit shifts has nothing to do with speed. Modern processors do integer multiply in one clock cycle. The test is about representation: candidates who can reach for n >> 1 and explain what the machine is doing have spent time thinking below the abstraction layer. That is the signal.

Writing n >> 1 is easy. Saying "right shift by one is floor division by two, and the bits that fall off are gone" in real time, under pressure, while also solving a problem, is a different skill entirely. That gap is exactly what practicing on SpaceComplexity is designed to close.

For a complete reference of every bit manipulation formula, see the Bit Manipulation Cheat Sheet.

Further Reading