题目描述:给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。
题目链接:201. 数字范围按位与
找出m和n的最长公共前缀(含位权)。
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int offset=0;
for (;m!=n;offset++){
m>>=1;
n>>=1;
}
return n<<offset;
}
};