-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitwiseANDOfNumbersRange.java
112 lines (87 loc) · 1.46 KB
/
BitwiseANDOfNumbersRange.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// https://leetcode.com/problems/bitwise-and-of-numbers-range/
// #bit-manipulation
class Solution {
public int rangeBitwiseAnd(int m, int n) {
// 1111000 <- remove Least significant: '1'
while (n > m) {
// reduce extremely n
// log n
n = n & (n - 1);
}
return n & m;
}
/* Time limited
public int rangeBitwiseAnd(int m, int n) {
int res = m;
// simplest AND BIT in range [m, n];
for (int i = m; i <= n; i++) {
// liner
res = res & i;
}
return res;
}
*/
/*
1000
0111
=> 0
|
1XXX10000
1XXX1YYYY
100
101
n <= m
1xx000
1xxyyy
=> m
|
100
101
110
111
110 =>
|
100 => 10X =>
|
0XX
m 100100000
1001XXXXX
n 100111111
12 -> 9
| |
1100 10XX
1011
1010
1001
|
1100
1111
0111
/*
5->9
9 & 8 & 7 & 6 & 5
1
1101
1100
1011
1010
1001
1000
0111
m = 7 => n = 13
|
1: 1100
2: 1000
10XX
3: 0000
0XXX
=> 1000 => 8
0XXX
n & (n-1)
=> xoá bit có giá trị là 1 đầu tiên của input (n)
n = 1010
= 1001
=> n & (n-1) = 1000
=>
*/
}