Learner Level
Implement the binaryStringToDecimal method that converts a binary string into its decimal integer value.
The input contains a string binary made of binary digits 0 and 1. Your task is to convert this binary number into its decimal integer value.
Each digit contributes based on its position from right to left. For example, 101 means 1×4 + 0×2 + 1×1, so the answer is 5.
Read the binary string from left to right and build the decimal value step by step.
Whenever a new binary digit is read, multiply the current decimal value by 2 and add the current digit. This works because moving one position in binary is the same as shifting the value left by one bit.
Pseudocode:
function binaryStringToDecimal(binary):
decimal = 0
for each character ch in binary:
digit = numeric value of ch
decimal = decimal * 2 + digit
return decimal