-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bitwise_201.java
33 lines (32 loc) · 957 Bytes
/
Bitwise_201.java
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
32
33
public class Bitwise_201 {
public static int rangeBitwiseAnd(int m, int n) {
/* 1. last bit of (odd number & even number) is 0.
2. when m != n, There is at least an odd number and an even number, so the last bit position result is 0.
3. Move m and n rigth a position.
if(m == 0){
return 0;
}
int moveFactor = 1;
while(m != n){
m >>= 1;
n >>= 1;
moveFactor <<= 1;
}
return m * moveFactor;
*/
int res = 0;
for (int i=31; i>=0; i--) {
if (getBit(m,i) && getBit(n,i))
res |= (1<<i);
if (!getBit(m,i) && getBit(n,i))
break;
}
return res;
}
public static boolean getBit(int n, int i) {
return (n&(1<<i)) != 0;
}
public static void main (String args[]) {
System.out.println(rangeBitwiseAnd(0,1));
}
}