Skip to main content
Back to problems
Leetcode
Medium
Bit Manipulation
Math
Bitwise And Of Numbers Range

Compute the bitwise AND of every integer in an inclusive range.

Acceptance 0%
Problem Statement

Given two integers left and right, return the bitwise AND of all numbers in the inclusive range [left, right].

The result is the value obtained by AND-ing left, left + 1, left + 2, ..., right together.

The key challenge is to avoid iterating through the entire range when it is large.

Input Format

  • Two integers left and right.
  • It is guaranteed that 0 <= left <= right.

Output Format

  • Return a single integer: the bitwise AND of all integers in [left, right].

Constraints

  • 0 <= left <= right
  • The range may be large, so an efficient solution is expected.
Examples
Sample cases returned by the problem API.

Example 1

Input

left = 5, right = 7

Output

4

Explanation

Binary forms: 5 = 101, 6 = 110, 7 = 111. The AND is 100, which is 4.

Example 2

Input

left = 0, right = 0

Output

0

Explanation

The range contains only one number, so the answer is 0.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.