Update BrianKernighanAlgorithm.java
This commit is contained in:
parent
5cf220362a
commit
1567b58a46
@ -8,14 +8,11 @@ import java.util.Scanner;
|
|||||||
*
|
*
|
||||||
* algorithm to count the number of set bits in a given number
|
* algorithm to count the number of set bits in a given number
|
||||||
*
|
*
|
||||||
* Subtraction of 1 from a number toggles all the bits (from
|
* Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the
|
||||||
* right to left) till the rightmost set bit(including the
|
|
||||||
* rightmost set bit).
|
* rightmost set bit).
|
||||||
* So if we subtract a number by 1 and do bitwise & with
|
* So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit.
|
||||||
* itself i.e. (n & (n-1)), we unset the rightmost set bit.
|
|
||||||
*
|
*
|
||||||
* If we do n & (n-1) in a loop and count the no of times loop
|
* If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count.
|
||||||
* executes we get the set bit count.
|
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* Time Complexity: O(logn)
|
* Time Complexity: O(logn)
|
||||||
|
Loading…
Reference in New Issue
Block a user