loading

Bitwise AND of Numbers Range — Step-by-Step Visualization

mediumLeetCode #201Bit Manipulation

Return bitwise AND of all numbers in range [left, right].

Algorithm Pattern

Common Bit Prefix

Key Idea

The range spans multiple values so all differing bits become 0; only the common leading prefix survives.

Step-by-Step Approach

  1. While left != right:
  2. left >>= 1, right >>= 1, shift++
  3. Return left << shift

Related Problems