We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8db4ef9 commit 5cf2203Copy full SHA for 5cf2203
Others/BrianKernighanAlgorithm.java
@@ -5,16 +5,18 @@
5
* @author Nishita Aggarwal
6
*
7
* Brian Kernighan’s Algorithm
8
+ *
9
* algorithm to count the number of set bits in a given number
10
11
* Subtraction of 1 from a number toggles all the bits (from
12
* right to left) till the rightmost set bit(including the
13
* rightmost set bit).
14
* So if we subtract a number by 1 and do bitwise & with
- * itself (n & (n-1)), we unset the rightmost set bit.
15
+ * itself i.e. (n & (n-1)), we unset the rightmost set bit.
16
17
* If we do n & (n-1) in a loop and count the no of times loop
18
* executes we get the set bit count.
- * Number of iterations of the loop is equal to the number of
- * set bits in a given integer.
19
20
21
* Time Complexity: O(logn)
22
0 commit comments