publicintrangeBitwiseAnd(int left, int right) {
while(right > left) {
right = right & (right-1);
}
return left&right;
}
1
2
3
4
5
6
7
8
publicintrangeBitwiseAnd(int left, int right) {
int ans = right;
while(right > left) {
ans = right & (right-1);
right = ans;
}
return ans;
}
1
2
3
4
5
6
7
8
9
10
11
12
publicintrangeBitwiseAnd(int left, int right) {
//~ find next 2^n bigger than left and if that is within the range result would be zeroint psq = Integer.highestOneBit(left);
int next = psq << 1;
if(next > left && next <= right) {
return 0;
}
while(left < right) right = right&(right-1);
return right;
}