File tree 3 files changed +17
-3
lines changed
3 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -31,13 +31,19 @@ static int countSetBits(int number) {
31
31
/**
32
32
* Optimized version.
33
33
*
34
+ * Uses BRIAN KERNIGAN'S bit counting. Acc. to this, the right most/least significant set bit is unset
35
+ * in each iteration. The time complexity is proportional to the number of bits set.
36
+ *
37
+ * {@see http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity}
38
+ * {@see http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive}
39
+ *
34
40
* @param n
35
41
* @return
36
42
*/
37
43
static int countSetBits (long n ) {
38
44
int count = 0 ;
39
45
while (n > 0 ) {
40
- n &= n - 1 ;
46
+ n &= n - 1 ; // right most set bit in n is unset
41
47
count ++;
42
48
}
43
49
return count ;
Original file line number Diff line number Diff line change 7
7
* @date: 6/3/15
8
8
* @time: 11:35 PM
9
9
*/
10
+
11
+ /**
12
+ * Time Complexity: O(1)
13
+ * Space Complexity: O(1)
14
+ * <p/>
15
+ * Note: Works only for positive integers.
16
+ */
10
17
public class Multiply {
11
18
12
19
/**
Original file line number Diff line number Diff line change 17
17
public class Parity {
18
18
19
19
/**
20
- * Uses BRIAN KERNIGAN'S bit counting. The time complexity is
21
- * proportional to the number of bits set.
20
+ * Uses BRIAN KERNIGAN'S bit counting. Acc. to this, the right most/least significant set bit is unset
21
+ * in each iteration. The time complexity is proportional to the number of bits set.
22
+ *
22
23
* {@see http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity}
23
24
* {@see http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive}
24
25
*
You can’t perform that action at this time.
0 commit comments