Suppose we have a number n, we have to find the number of bit 1 present in the binary representation of that number.
So, if the input is like 12, then the output will be 2
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): count = 0 while (n): count += n & 1 n >>= 1 return count ob = Solution() print(ob.solve(12))