Problem Walkthrough

Bitwise AND Range LeetCode 201 Guide

Find the bitwise AND of all numbers in a range by right-shifting left and right until they share a common binary prefix — then shift the result back left.

8 min read|

Bitwise AND Range

LeetCode 201

Problem Overview

LeetCode 201 — Bitwise AND of Numbers Range — asks you to return the bitwise AND of all integers in the inclusive range [left, right]. For example, given left = 5 and right = 7, the answer is 4 because 5 & 6 & 7 = 4.

The range can span up to 2^31 integers, so iterating from left to right computing AND is completely infeasible. You need an O(log n) insight that collapses the range into a single operation.

Despite its Medium label, this problem is a pure bit-manipulation puzzle. Once you see the key insight — that the AND of a range preserves only the common binary prefix of its endpoints — the solution writes itself.

  • Input: two non-negative integers left and right where left <= right
  • Output: the bitwise AND of every integer from left to right inclusive
  • Constraint: 0 <= left <= right <= 2^31 - 1
  • A brute-force loop would require up to 2 billion iterations — completely infeasible
  • Expected complexity: O(log n) time, O(1) space using bit manipulation

Key Insight — The Common Binary Prefix

The AND of any two consecutive integers always clears the lowest differing bit. Think about it: if two numbers differ at bit position k, then every value in the range between them cycles through both 0 and 1 at position k. Once a bit position has both a 0 and a 1 anywhere in the range, its AND contribution is 0.

This means the AND of the entire range [left, right] is exactly the common binary prefix shared by left and right. All bits below the highest differing bit position become 0 after the AND. The bits above that position — where left and right agree — are preserved.

For example, 5 = 101 and 7 = 111. They differ starting at bit 1. The common prefix at bit 2 is 1, giving result 100 = 4. Every bit below the common prefix is zeroed out by the AND of the values in between.

💡

Key Insight

The AND of a range only preserves the bits where left and right agree in their binary representation. Any bit position where they differ will have both 0 and 1 appear somewhere in the range, making that bit's AND contribution = 0.

Right-Shift Approach

The right-shift approach works by repeatedly shifting both left and right one bit to the right until they become equal. At that point, both numbers share all remaining bits — meaning those bits form the common prefix. You then shift the result back left by the number of shifts performed.

Each shift removes the lowest bit from both numbers. Because differing lower bits are discarded, the two numbers converge toward their common prefix. Once left equals right, you have isolated exactly the bits that survive the AND of the full range.

This approach is elegant in its simplicity: count the shifts, find the equal value, shift it back. The time complexity is O(32) for 32-bit integers — effectively O(1) — and space is O(1).

  1. 1Initialize a shift counter to 0
  2. 2While left != right: shift both left and right one bit to the right; increment shift counter
  3. 3When left == right, you have found the common binary prefix
  4. 4Return left (or right, they are equal) shifted back left by the shift count
  5. 5Example: left=5(101), right=7(111) → shift 1: (10, 11) → shift 2: (1, 1) → equal; return 1 << 2 = 4

Brian Kernighan Approach

An alternative uses the Brian Kernighan bit-clearing trick: right = right & (right - 1) clears the lowest set bit of right. By repeatedly clearing the lowest set bit of right while right is greater than left, you progressively strip away bits that differ between right and left.

When right finally drops to or below left, you have removed all the bits that are not part of the common prefix. At that point, right itself is the common prefix — which is exactly the AND of the entire range.

Both approaches run in O(32) time and O(1) space. The Brian Kernighan version tends to converge faster in practice when left and right differ in only a few low bits, since it clears one set bit per iteration rather than always shifting by exactly one.

ℹ️

Brian Kernighan Trick

Brian Kernighan's trick (n & (n-1) clears the lowest set bit) progressively strips the rightmost 1 bit from right until it drops to or below left. The remaining value is the common binary prefix — which is the AND of the entire range.

Walk-Through Example — left=5, right=7

Let us trace through the right-shift approach with left = 5 (binary: 101) and right = 7 (binary: 111). These differ at bits 0 and 1, so we expect those bits to be zeroed out, leaving 100 = 4.

We start the shift counter at 0. In the first iteration, 5 != 7, so we shift: left becomes 10 (2), right becomes 11 (3), shift count is now 1. Still not equal, so we shift again: left becomes 1, right becomes 1, shift count is 2. Now left == right, so we stop.

The common prefix is 1. We shift it back: 1 << 2 = 4. We can verify: 5 & 6 & 7 = 101 & 110 & 111 = 100 = 4. Correct! The two lower bits were zeroed because both 0 and 1 appear at those positions within the range.

  1. 1Start: left=5 (101), right=7 (111), shifts=0
  2. 2Iteration 1: left=2 (10), right=3 (11), shifts=1 — still not equal
  3. 3Iteration 2: left=1 (1), right=1 (1), shifts=2 — equal, stop
  4. 4Common prefix = 1; result = 1 << 2 = 4 (100 in binary)
  5. 5Verify: 5 & 6 & 7 = 101 & 110 & 111 = 100 = 4 — correct!

Code Walkthrough — Python and Java

Both the right-shift approach and the Brian Kernighan approach are clean and concise. The Python right-shift version: initialize shifts=0; while left!=right: left>>=1, right>>=1, shifts+=1; return left<<shifts. That is four lines. The Brian Kernighan Python version: while right>left: right&=right-1; return right. Two lines.

In Java the code is identical in structure. The right-shift version: int shifts=0; while(left!=right){left>>=1; right>>=1; shifts++;} return left<<shifts;. The Kernighan version: while(right>left) right&=right-1; return right;. Both run in O(log n) — more precisely O(32) = O(1) for 32-bit integers — and use O(1) space.

Either approach is acceptable in an interview. The right-shift version is perhaps easier to explain from first principles. The Kernighan version is shorter and shows familiarity with classic bit tricks. Mentioning both and explaining why they are equivalent will impress interviewers.

⚠️

Do Not Iterate the Range

Never iterate from left to right computing AND one step at a time. The range can be up to 2^31 ≈ 2 billion iterations. The bit-manipulation approach runs in at most 32 iterations regardless of the range size — that is the entire point of O(32) = O(1).

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now