ExamAdda Logo

Bitwise AND of Numbers Range

Easy

Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, including both left and right.

Example 1

Input

left = 5
right = 7

Output

4

Explanation

The numbers in the range are:

5 = 101
6 = 110
7 = 111

 

Their bitwise AND is:

101
110
111
---
100

 

So the answer is 4.

Example 2

Input

left = 0
right = 0

Output

0

Explanation

The range contains only 0, so the result is 0.

Constraints

0 <= left <= right <= 2^31 - 1

Hints:

Hint 1

Think about what happens to the common leading bits of all numbers between left and right.

Hint 2

Any bit that changes somewhere in the range will become 0 in the final AND.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 23, 2026

Last updated

Aug 23, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output