Number of 1 Bits
Welcome to the exciting world of Bitland! Today, you are going to explore how computers think in terms of bits. Imagine you have a magical number that secretly hides binary information, like a special code. Your task is to find out how many 1s are in that code!
A "bit" is the smallest piece of data in a computer, represented by either 0 or 1. Every number can be represented in binary format, which is just a series of bits.
Your job is to write a function count_one_bits that takes an integer (our magical number) and returns the number of 1s in its binary representation. Remember, computers love when you give them clean and simple answers, so keep your function neat!
Example
Let's see some examples that might come across a computer's path:
count_one_bits(5) # 5 in binary is '101', so there are 2 ones # Output: 2 count_one_bits(9) # 9 in binary is '1001', so there are 2 ones # Output: 2 count_one_bits(15) # 15 in binary is '1111', so there are 4 ones # Output: 4
Sounds like fun? Let's jump into it and start counting!
Tip: You can use Python's handy bin() function to convert a number to a binary string but you'll need to handle the prefix '0b'.
Inputs
Arg 1:
5Expected Output
2Actual Output