Cover image for Classic 150 Interview Questions P201 Bitwise AND of Numbers Range

Classic 150 Interview Questions P201 Bitwise AND of Numbers Range


Timeline

Timeline

2025-11-25

init

Bit manipulation

Problem:

Find the common binary prefix and then shift it back to the left.

123456789101112131415
class Solution {    public:	int rangeBitwiseAnd(int left, int right)	{		int shift = 0;		while (left < right) {			left = left >> 1;			right = right >> 1;			shift++;		}		return right << shift;	}};
Loading comments…