Count Set Bits
EasyPractice library · +10 XPbit manipulationloopsbasic
Given a single non-negative integer N, print the number of bits that are set to 1 in its binary representation. For example, if N is 13, its binary representation is 1101, which has three 1s. So the output should be 3.
Input
A single non-negative integer N.
Output
A single integer representing the count of set bits in N.
Constraints
0 <= N <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
13
Output
3
Binary of 13 is 1101, which has three 1s.
Example 2
Input
0
Output
0
Binary of 0 is 0, which has zero 1s.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
13
Example 2 input
0