-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_201.java
More file actions
31 lines (27 loc) · 688 Bytes
/
Copy pathP_201.java
File metadata and controls
31 lines (27 loc) · 688 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package leetcode.medium;
public class P_201 {
public int rangeBitwiseAndOld(int m, int n) {
int c = 0;
while (m != n) {
m >>= 1;
n >>= 1;
c++;
}
return m << c;
}
public int rangeBitwiseAndBC(int m, int n) {
while (m < n) {
n &= n - 1;
}
return m & n;
}
public int rangeBitwiseAnd(int left, int right) {
int res = 0;
for (int shift = 31; shift >= 0 && (left & (1 << shift)) == (right & (1 << shift)); shift--) {
if ((left & (1 << shift)) != 0) {
res |= 1 << shift;
}
}
return res;
}
}