Optimal Algorithmic Approach
Brian Kernighan: n = n & (n-1) loop, counting iterations.
Core Invariants & Edge Cases
- n & (n-1) clears the lowest set bit each iteration
- Repeat until n = 0; count iterations
- Brian Kernighan's Algorithm: O(number of set bits)