Problem 793
Binary to Decimal
Easy · 10 pts · Math, Basics, Bit Manipulation
Problem
Convert a binary number (a string of 0s and 1s) to decimal. Each digit is worth a power of two, with the rightmost digit worth 1, the next 2, then 4, and so on. Process the digits from left to right with value = value × 2 + digit. The binary string has at most 60 digits, so the result fits in a 64-bit integer.
Input
A single binary string B (leading zeros are allowed).
Output
Print the decimal value of B.
Constraints
1 <= |B| <= 60
Example input
1011
Example output
11
Explanation
The digits of 1011 are worth 8, 4, 2 and 1 from left to right, so the value is 1 × 8 + 0 × 4 + 1 × 2 + 1 × 1 = 11.