Leetcode 201 Solution
This article provides solution to leetcode question 201 (bitwise-and-of-numbers-range)
Access this page by simply typing in "lcs 201" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/bitwise-and-of-numbers-range
Solution
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int res = 0;
for (int i = 31; i >= 0; i--)
{
int bit1 = (m >> i) & 1;
int bit2 = (n >> i) & 1;
if (bit1 == 0 && bit2 == 0)
continue;
else if (bit1 == 0 || bit2 == 0)
break;
else
res |= (1 << i);
}
return res;
}
};