The direct approach: loop through all 32 bit positions (assuming 32-bit integers) and check if each bit is set. Count how many times you find a 1.
For each position from 0 to 31, check if . If true, increment your count.
This works, but you're always checking 32 bits even if has only a few bits set. Can you do better? What if you only looked at the bits that are actually 1?