Counting Bits
Advanced
Welcome to the 'Counting Bits' challenge! In this challenge, your task is to write a function that counts the number of 1s in the binary representation of a given positive integer. This can help kids learn about binary numbers, which is a fundamental concept in computer science.
What You'll Do:
Write a Python function count_bits(n) that counts how many times the digit 1 appears in the binary representation of the number n.
Example:
Here's a quick example to help you understand:
# The binary representation of 5 is '101', which contains two '1's. print(count_bits(5)) # Output should be 2 # The binary representation of 13 is '1101', which contains three '1's. print(count_bits(13)) # Output should be 3
Fun Fact:
Did you know that almost all digital electronics use the binary number system to operate? That's why learning how to count bits is like finding out how a computer thinks in number games!
Are you ready? Let's jump into the world of bits and figures!
Inputs
Arg 1:
5Expected Output
2Actual Output