Free tools Windows power users keep installed
One-click scans. No signup required.
Convert each binary string to a Python integer with base 2, add the values, then format the sum in base 2. The program below accepts non-empty strings made only of 0 and 1, allows leading zeros, and prints the result without a 0b prefix.
Python program using integer conversion
This version is concise and practical when the goal is to add two binary values, rather than to implement binary arithmetic digit by digit.
As an Amazon Associate I earn from qualifying purchases.
def add_binary(a: str, b: str) -> str:
if not a or not b or any(bit not in "01" for bit in a + b):
raise ValueError("Inputs must be non-empty binary strings")
return format(int(a, 2) + int(b, 2), "b")
first = input("Enter the first binary number: ").strip()
second = input("Enter the second binary number: ").strip()
try:
print("Sum:", add_binary(first, second))
except ValueError as error:
print(error)
For example, entering 1011 and 1101 prints Sum: 11000. Those inputs represent decimal 11 and 13, whose sum is decimal 24, or binary 11000.
How the conversion works
int(a, 2)interprets the text inaas a base-2 integer. For example,int("1011", 2)is 11. See the Python documentation forint.- The
+operator adds the resulting integer values. format(total, "b")converts the sum to binary digits. The Python documentation definesbin(x)as converting an integer to a binary string prefixed with0b; usingformat(total, "b")avoids that prefix for this non-negative result. See Python’sbindocumentation.
Input behavior and validation
- Accepted: one or more characters, all of which must be
0or1. - Leading zeros: accepted. For example,
001and1are both interpreted as the value one; the output is the normalized binary sum, without unnecessary leading zeros. - Empty input or other characters: rejected with a
ValueErrormessage instead of being silently changed. - Signs: not accepted by this function, because its stated input format is binary digits only.
The input calls use .strip() to remove surrounding whitespace before validation. Avoid parsing untrusted numeric text with eval: it executes the supplied code rather than simply parsing a number, as noted by the Python Wiki’s discussion of eval.
#1 Best Overall
When to use a manual carry algorithm instead
Integer conversion is the appropriate choice for a short utility. If an exercise specifically asks you to implement binary addition, use a right-to-left digit algorithm: add corresponding bits and a carry, write the result bit, and carry any remaining value into the next column. That approach makes the arithmetic visible; conversion through int and format delegates it to Python’s built-ins.
Python integers are not restricted to a fixed machine-word range: their practical size is limited by available memory, according to the Python documentation on numeric types. A direct digit algorithm can avoid constructing large integer objects for extremely long input strings, but this does not establish that it will be faster for a particular workload.
Quick Recap
Best Value
Rank #2
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errors




