Concept Flow - Count Set Bits Brian Kernighan Algorithm
Start with number n
Check if n != 0?
No→Stop, count is final
Yes
Perform n = n & (n-1)
Increment count by 1
↩Back to Check n != 0
The algorithm repeatedly removes the rightmost set bit from n by doing n = n & (n-1), counting how many times this happens until n becomes zero.
