Problems > Ones in Binary > Editorial

Ones in Binary — Solution & Editorial

Back to the Problem

Repeatedly clear the lowest set bit with n &= n-1, or use the built-in popcount.

Complexity: O(number of set bits)