让我们看下面的实现以更好地理解-
#include <bits/stdc++.h> using namespace std; class Solution { public: int rangeBitwiseAnd(int m, int n) { int i = 0; while(m != n){ m >>= 1; n >>= 1; i++; } return m << i; } }; main(){ Solution ob; cout << (ob.rangeBitwiseAnd(5,7)); }
5 7
输出结果
4