Bitwise AND of Numbers Range
Problem Statement
You are given two integers, left and right, with left β€ right. Take every whole number from left to right, both ends included. Return the bitwise AND of all of them together.
Two integers left and right, with left β€ right.
The AND of every integer from left to right, both included.
5 = 101, 6 = 110, 7 = 111. Their AND is 100 = 4. The number 6 clears the lowest bit that 5 and 7 share.
The only number is 0, so the AND is 0.
The range contains 1 (01) and 2 (10). They share no set bit, so every bit of the AND ends up 0.
- β’ 0 β€ left β€ right β€ 2^31 β 1
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Loop from left to right and AND each number into a running value
Shift both ends right until equal, then shift the common part back
Start with a running value that has every bit set. Loop from left to right and AND each number into it. When the loop ends, the running value is the answer.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.