Skip to content

Commit ffd8792

Browse files
author
hieu
committed
Solve Problem - BitWise AND of numbers range
1 parent d1796f3 commit ffd8792

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import slidingwindow.Problem992_SubarraysWithKDifferentIntegers;
1+
import bit.Problem_BitWiseANDofNumbersRange;
22

33
public class Main {
44

55
public static void main(String[] args) {
6-
Problem992_SubarraysWithKDifferentIntegers.run();
6+
Problem_BitWiseANDofNumbersRange.run();
77
}
88
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package bit;
2+
3+
public class Problem_BitWiseANDofNumbersRange {
4+
5+
public static void run() {
6+
Problem_BitWiseANDofNumbersRange solution = new Problem_BitWiseANDofNumbersRange();
7+
int a = 5, b = 7;
8+
int res = solution.rangeBitwiseAnd(a, b);
9+
System.out.println(res);
10+
}
11+
12+
public int rangeBitwiseAnd(int m, int n) {
13+
int i = 0; // i means we have how many bits are 0 on the right
14+
while(m != n){
15+
m >>= 1;
16+
n >>= 1;
17+
i++;
18+
}
19+
return m << i;
20+
}
21+
}

0 commit comments

Comments
 (0)