#201

Bitwise AND of Numbers Range

Medium Common prefix

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the bitwise AND of every integer from left to right inclusive.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: AND every number from left to right.

Why this works: any bit that changes somewhere in the range becomes 0. Only the shared starting bits (the common prefix) survive.

Example: left = 5 (101), right = 7 (111)

101 vs 111 → shift → 10 vs 11→ 1 vs 1 equal after 2 shifts1 << 2 = 100 = 4

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Looping from left to right and ANDing every number — the range can have two billion numbers.