How Many Bits Long
Return how many bits it takes to write n in binary without zeros in front. For example 5 is 101, which takes 3 bits, and 8 is 1000, which takes 4.
The number 0 is the exception: it has no 1 at all, and the answer for it is 0.
Example 1
n 5
returns 3
5 is 101.
Example 2
n 8
returns 4
8 is 1000.
Example 3
n 0
returns 0
By the rule for 0.
Constraints
0 <= n <= 10**9
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Keep the Low BitsBeginner
- Lowest Set BitBeginner
- Parity BitBeginner
- Read One BitBeginner
- Read a Binary StringBeginner
- Set Bit PositionsBeginner
- Switch Off the Lowest OneBeginner
- Add Without PlusEasy