Concept Flow - Count Set Bits Brian Kernighan Algorithm
Start with number n
Check if n != 0?
No→Stop
Yes
Remove rightmost set bit: n = n & (n-1)
Increment count
Repeat check
The algorithm repeatedly removes the rightmost 1-bit from the number and counts how many times this happens until the number becomes zero.