Number of 1 Bits - Brian Kernighan's Trick

The expression n & (n - 1) clears the lowest set bit.

Example: n=12n = 12 (binary 1100). n1=11n - 1 = 11 (binary 1011). n&(n1)=1000n \& (n-1) = 1000 (binary 1000).

Count how many times you can apply this operation until nn becomes 00. Each operation removes exactly one set bit.