0
0
Intro-computingHow-ToBeginner ยท 2 min read

How to Convert Binary to Decimal - Computing Fundamentals

To convert binary to decimal, multiply each binary digit by 2 raised to the power of its position index from right to left starting at 0, then add all the results together, like decimal = sum(bit * 2**position).
๐Ÿ“‹

Examples

Input101
Output5
Input1101
Output13
Input0
Output0
๐Ÿง 

How to Think About It

Think of each binary digit as a switch that is either off (0) or on (1). Each position from right to left represents a power of 2 starting at 0. To find the decimal number, add up the values of all the switches that are on.
๐Ÿ“

Algorithm

1
Get the binary number as a string.
2
Start from the rightmost digit and move left.
3
For each digit, convert it to a number (0 or 1).
4
Multiply this number by 2 raised to the power of the digit's position index.
5
Add this value to a running total.
6
Return the total as the decimal number.
๐Ÿ’ป

Code

computing_fundamentals
binary = input('Enter binary number: ')
decimal = 0
for i, bit in enumerate(reversed(binary)):
    decimal += int(bit) * (2 ** i)
print('Decimal:', decimal)
Output
Enter binary number: 1101 Decimal: 13
๐Ÿ”

Dry Run

Let's trace binary '101' through the code

1

Start with decimal = 0

decimal = 0

2

Process rightmost bit '1' at position 0

decimal += 1 * 2**0 = 1

3

Process middle bit '0' at position 1

decimal += 0 * 2**1 = 0 (no change)

4

Process leftmost bit '1' at position 2

decimal += 1 * 2**2 = 4

StepBitPositionCalculationDecimal Total
1101 * 2**0 = 11
2010 * 2**1 = 01
3121 * 2**2 = 45
๐Ÿ’ก

Why This Works

Step 1: Binary digits represent powers of 2

Each digit in a binary number stands for 2 raised to the power of its position, starting from 0 on the right.

Step 2: Multiply each bit by its power of 2

Multiply the bit (0 or 1) by 2 to the power of its position to find its decimal value.

Step 3: Sum all values to get decimal

Add all these values together to get the final decimal number.

๐Ÿ”„

Alternative Approaches

Using built-in conversion function
computing_fundamentals
binary = input('Enter binary number: ')
decimal = int(binary, 2)
print('Decimal:', decimal)
This method is simpler and faster but depends on language built-ins.
Recursive conversion
computing_fundamentals
def bin_to_dec(b):
    if b == '':
        return 0
    return bin_to_dec(b[:-1]) * 2 + int(b[-1])
binary = input('Enter binary number: ')
print('Decimal:', bin_to_dec(binary))
Uses recursion to process bits from left to right; good for understanding but less efficient.
โšก

Complexity: O(n) time, O(1) space

Time Complexity

The algorithm processes each binary digit once, so time grows linearly with the number of digits.

Space Complexity

Only a few variables are used regardless of input size, so space usage is constant.

Which Approach is Fastest?

Using built-in functions is fastest and simplest; manual loops give more control and understanding.

ApproachTimeSpaceBest For
Manual loopO(n)O(1)Learning and control
Built-in functionO(n)O(1)Quick and practical use
Recursive methodO(n)O(n)Educational, less efficient
๐Ÿ’ก
Start counting positions from right to left, beginning at zero, when converting binary to decimal.
โš ๏ธ
Forgetting to reverse the binary string or miscounting the position index leads to wrong decimal results.