Binary (base 2) uses only bits 0 and 1. Decimal (base 10) is the everyday number system. Conversion means reading each bit as a power of two and summing the contributions of every 1.
Remember
Rightmost bit = 2⁰ (value 1)
Next bit = 2¹ (value 2)
Next bit = 2² (value 4)
…
Example: 101010
1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42
Binary
Place values used
Decimal
1010
8 + 2
10
1111
8 + 4 + 2 + 1
15
101010
32 + 8 + 2
42
Approach
How to Convert Binary to Decimal in Python
Four clear steps — then pick a manual loop or int(s, 2).
Read the binary string (only 0 and 1 allowed).
Validate every character before converting.
Sum place values (right-to-left), or call int(bits, 2).
Print or return the decimal result.
Method
Idea
Time
Best for
Place-value loop
Walk right-to-left; add 2^power for each 1
O(k)
Interviews, learning place values
int(s, 2)
Parse the string as base 2 in one call
O(k)
Production code, short answers
Pseudocode
Pseudocode
function BinaryToDecimal(bits):
if bits is empty:
reject input
decimal = 0
power = 1 // 2⁰
for i from length(bits) - 1 down to 0:
ch = bits[i]
if ch is not '0' and not '1':
reject input
if ch == '1':
decimal = decimal + power
power = power * 2
return decimal
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Valid bits
Only 0 and 1
Place values
1, 2, 4, 8, 16, 32, 64, 128, …
Start side
Rightmost bit = 2⁰
Built-in
int(bits, 2)
Classic check
101010 → 42
Try it
Live Preview
Enter a binary string to see place values and the decimal result.
Digits 0 and 1 only (up to 31 bits). Tap a chip or type a value.
Result
Binary = 101010
Places = 32 + 8 + 2
Decimal = 42
Trace
Worked Walkthrough — 101010
Start at the right. Multiply each bit by its place value, then add the non-zero terms.
Start with the place-value loop so you can explain every step. Then use int(s, 2) for a one-line production-style answer.
Example 1 — Place-Value Loop
Walk the string from right to left. Double the place value after each bit.
python
def binary_to_decimal(bits: str) -> int:
bits = bits.strip()
if not bits:
raise ValueError("Binary string cannot be empty.")
decimal_value = 0
power = 1
for ch in reversed(bits):
if ch not in ("0", "1"):
raise ValueError("Only digits 0 and 1 are allowed.")
if ch == "1":
decimal_value += power
power *= 2
return decimal_value
binary = "101010"
print(f"{binary} → {binary_to_decimal(binary)}")
Output
101010 → 42
How It Works
Reject an empty string before any math begins.
reversed(bits) starts at the rightmost bit with power = 1 (that is 20).
For each character, accept only '0' or '1'; anything else raises ValueError.
If the bit is '1', add the current power. A '0' adds nothing.
Double power after each bit. For 101010, the total is 2 + 8 + 32 = 42.
Example 2 — int(s, 2)
Validate the bits, then let Python parse the string as base 2.
python
def binary_to_decimal(bits: str) -> int:
bits = bits.strip()
if not bits:
raise ValueError("Binary string cannot be empty.")
if any(ch not in "01" for ch in bits):
raise ValueError("Only digits 0 and 1 are allowed.")
return int(bits, 2)
print(binary_to_decimal("101010")) # 42
print(binary_to_decimal("1111")) # 15
Output
42
15
How It Works
Reject an empty string immediately.
Scan every character once. If any digit is not 0 or 1, raise — this stops a confusing ValueError from int later.
Call int(bits, 2). The second argument means “treat this string as base 2.”
The call returns a normal decimal int (for example "101010" → 42, "1111" → 15).
Prefer the manual loop in interviews when they want the math; prefer int(s, 2) in real projects.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
Reject empty input
Raise ValueError — never convert silently.
0 / 1 only
Validate every character
2, spaces, or letters cause ValueError in int(s, 2).
Leading 0
Still valid
00101 equals 5. Leading zeros do not change the value.
Long bits
Python int grows
Unlike fixed-width languages, Python integers expand with memory — still validate input length in interviews if asked.
Analysis
Time and Space Complexity
Program
Time
Extra space
Place-value loop
O(k)
O(1)
int(s, 2)
O(k)
O(1)
k is the number of bits in the input string.
Remember
Key Takeaways
Place values: rightmost bit is 2⁰; only 1-bits add to the total.
Validate first: reject empty strings and any character outside 0/1.
Two options: manual place-value loop, or int(bits, 2).
Test set:101010 → 42; 1111 → 15; 0 → 0.
One line: sum powers of two for each 1-bit — or call int(bits, 2).
Frequently Asked Questions
Sum powers of two for each 1-bit, or call int(bits, 2). Both turn a base-2 string into a base-10 integer.
It parses string s as a base-2 number and returns its decimal int value. Python ints grow as needed for long bit strings.
Binary may contain only 0 and 1. Validation avoids ValueError and wrong answers from invalid characters.
No. Strings like 00101 are valid and equal 5 in decimal.
Both the manual loop and int(s, 2) are O(k), where k is the number of bits. Extra space is O(1) beyond the input.