Skip to content

Commit 9e00e5d

Browse files
authored
Update Bitwise AND of Numbers Range.java
1 parent 6843b57 commit 9e00e5d

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed
Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,8 @@
11
class Solution {
2-
public int rangeBitwiseAnd(int m, int n) {
3-
int temp1 = m;
4-
int temp2 = n;
5-
6-
int cnt1 = 0;
7-
int cnt2 = 0;
8-
9-
while (temp1 > 1 || temp2 > 1) {
10-
if (temp1 > 1) {
11-
temp1 /= 2;
12-
cnt1++;
13-
}
14-
15-
if (temp2 > 1) {
16-
temp2 /= 2;
17-
cnt2++;
18-
}
19-
}
20-
21-
if (cnt1 != cnt2) {
22-
return 0;
23-
}
24-
25-
int res = ~0;
26-
for (int i=m;i<n;i++) {
27-
res = res&i;
28-
}
29-
30-
res = res&n;
31-
32-
return res;
2+
public int rangeBitwiseAnd(int left, int right) {
3+
while (right > left) {
4+
right = right & (right - 1);
335
}
6+
return left & right;
7+
}
348
}

0 commit comments

Comments
 (0)