Count Set Bits
Programming Interview
Medium
5 views
Problem Description
One non-negative integer n is provided. Count how many 1 bits are in binary representation and output it.
Input Format
One integer n.
Output Format
One integer count.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=int(s)
c=0
while n:
n&=n-1
c+=1
sys.stdout.write(str(c))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!