Skip to content

Commit 74a9395

Browse files
author
Ram swaroop
committed
added comments
1 parent bfb8ae9 commit 74a9395

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/me/ramswaroop/bits/CountSetBits.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ static int countSetBits(int number) {
3131
/**
3232
* Optimized version.
3333
*
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+
*
3440
* @param n
3541
* @return
3642
*/
3743
static int countSetBits(long n) {
3844
int count = 0;
3945
while (n > 0) {
40-
n &= n - 1;
46+
n &= n - 1; // right most set bit in n is unset
4147
count++;
4248
}
4349
return count;

src/me/ramswaroop/bits/Multiply.java

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
* @date: 6/3/15
88
* @time: 11:35 PM
99
*/
10+
11+
/**
12+
* Time Complexity: O(1)
13+
* Space Complexity: O(1)
14+
* <p/>
15+
* Note: Works only for positive integers.
16+
*/
1017
public class Multiply {
1118

1219
/**

src/me/ramswaroop/bits/Parity.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
public class Parity {
1818

1919
/**
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+
*
2223
* {@see http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity}
2324
* {@see http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive}
2425
*

0 commit comments

Comments
 (0)