VOOZH about

URL: https://www.geeksforgeeks.org/dsa/bitwise-and-or-of-a-range/

⇱ Bitwise AND of elements in range - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Bitwise AND of elements in range

Last Updated : 25 Nov, 2025

Given two positive integers, l and r such that l ≤ r, find bit-wise AND of all integers from l to r, i.e., we need to compute value of l & (l+1) & ... & (r-1) & r, where & denotes bitwise AND.
Examples:

Input: l = 10, r = 15
Output: 8
Explanation: 10 & 11 & 12 & 13 & 14 & 15 = 8

Input: l = 12, r = 15
Output: 12
Explanation: 12 & 13 & 14 & 15 = 12

[Naive Approach] Traversing all elements in range - O(y - x) Time and O(1) Space

We will start from l and keep AND-ing it with every next number (l+1, l+2, …, r). As we move forward, whenever any number in this range has a 0 in some bit position, that bit becomes 0 in the result. This continues until we reach r, giving the final AND of all numbers.


Output
8

[Expected Approach - 1] Checking For All Bits - O(log r) Time and O(1) Space

The idea is to check each bit independently and decide whether that bit can stay 1 in the final AND. A bit remains 1 only if every number from l to r has that bit set. So for bit k, we first ensure that both l and r have that bit as 1. Next, we check whether the range crosses the boundary where that bit flips from 1 to 0.

This boundary appears at multiples of 2k, because below 2k the k-th bit is 0 and above it the k-th bit becomes 1. If the range size is smaller than 2k, we do not cross this boundary, so every number keeps that bit set. In that case, we can safely include that bit in the final result.


Output
8

[Expected Approach - 2] Checking MSB - O(log r) Time and O(1) Space

The idea is that the AND of all numbers from l to r preserves only those higher-order bits where l and r are identical. We scan bits from the most significant bit downward. If both l and r have bit k set, then every number in the interval must lie inside the same block of size 2^(k+1) where bit k stays 1, so we safely keep that bit in the answer.

But if at some bit position one of l or r has the bit set and the other has it unset, it means the range crosses the boundary where bit k flips from 1 to 0. This boundary occurs every 2^k numbers. When the interval crosses such a boundary, there must be at least one number inside the range with that bit equal to 0. Once that happens, not only does bit k drop to 0 in the final AND, but all lower bits also lose their stability because they flip even more frequently. Therefore every bit below k is guaranteed to become 0, so we stop processing further.


Output
8

[Expected Approach - 3] Right Shifting Until l and r become equal - O(log l) Time and O(1) Space

The idea is that the AND of all numbers from l to r keeps only the bits where l and r share the same prefix. As long as l and r differ, some number in the range will force those lower bits to become 0. So we repeatedly right-shift both l and r until they become equal, which removes all unstable lower bits. The number of shifts tells us how many lower bits must be zero in the final answer. Once they match, we left-shift back to place the common prefix in its correct position, giving the AND of the whole range.


Output
8


Comment
Article Tags:
Article Tags: