Read a Binary String
bits is a number written in binary, as a string of the characters 0 and 1 with the highest bit first. Return the number.
Work it out yourself, bit by bit, without a ready-made conversion. For example "1011" is 8 + 2 + 1 = 11. Zeros in front change nothing: "0011" is 3.
Example 1
bits "1011"
returns 11
8 + 0 + 2 + 1.
Example 2
bits "0011"
returns 3
The two zeros in front add nothing.
Example 3
bits "0"
returns 0
A single 0.
Constraints
1 <= len(bits) <= 30bitsholds only0and1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Bits problems
- Set Bit PositionsBeginner
- Switch Off the Lowest OneBeginner
- Add Without PlusEasy
- Alternating BitsEasy
- Binary GapEasy
- Bitwise AND of a RangeEasy
- Block of OnesEasy
- Gray PositionEasy