Skip to content

Commit 9684fc7

Browse files
author
hieu
committed
Add more details
1 parent ffd8792 commit 9684fc7

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/bit/Problem_BitWiseANDofNumbersRange.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ public static void run() {
99
System.out.println(res);
1010
}
1111

12+
/*
13+
For example, for number 26 to 30
14+
Their binary form are:
15+
16+
11010
17+
11011
18+
11100  
19+
11101  
20+
11110
21+
22+
Because we are trying to find bitwise AND, so if any bit there are at least one 0 and one 1, it always 0. In this case, it is 11000.
23+
So we are go to cut all these bit that they are different. In this case we cut the right 3 bit.
24+
*/
1225
public int rangeBitwiseAnd(int m, int n) {
1326
int i = 0; // i means we have how many bits are 0 on the right
1427
while(m != n){

0 commit comments

Comments
 (0)