 Open in App
Not now

# Python program to add two binary numbers

• Difficulty Level : Easy
• Last Updated : 05 Mar, 2023

Given two binary numbers, write a Python program to compute their sum.

Examples:

```Input:  a = "11", b = "1"
Output: "100"

Input: a = "1101", b = "100"
Output: 10001```

Approach:

• Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum becomes more than 1, then store carry for the next digits.
• Using inbuilt function: Calculate the result by using the inbuilt bin() and int() function.

Method 1: Naive Approach:

The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum becomes more than 1, then store carry for the next digits.

## Python3

 `# Python program to add two binary numbers.` `# Driver code``# Declaring the variables``a ``=` `"1101"``b ``=` `"100"``max_len ``=` `max``(``len``(a), ``len``(b))``a ``=` `a.zfill(max_len)``b ``=` `b.zfill(max_len)` `# Initialize the result``result ``=` `''` `# Initialize the carry``carry ``=` `0` `# Traverse the string``for` `i ``in` `range``(max_len ``-` `1``, ``-``1``, ``-``1``):``    ``r ``=` `carry``    ``r ``+``=` `1` `if` `a[i] ``=``=` `'1'` `else` `0``    ``r ``+``=` `1` `if` `b[i] ``=``=` `'1'` `else` `0``    ``result ``=` `(``'1'` `if` `r ``%` `2` `=``=` `1` `else` `'0'``) ``+` `result` `    ``# Compute the carry.``    ``carry ``=` `0` `if` `r < ``2` `else` `1` `if` `carry !``=` `0``:``    ``result ``=` `'1'` `+` `result` `print``(result.zfill(max_len))`

Output

`10001`

Time complexity : O(n)

Space Complexity : O(n)

Method 2: Using inbuilt functions:

We will first convert the binary string to a decimal using int() function in python. The int() function in Python and Python3 converts a number in the given base to decimal. Then we will add it and then again convert it into a binary number using bin() function.

Example 1:

## Python3

 `# Python program to add two binary numbers.` `# Driver code``# Declaring the variables``a ``=` `"1101"``b ``=` `"100"` `# Calculating binary value using function``sum` `=` `bin``(``int``(a, ``2``) ``+` `int``(b, ``2``))` `# Printing result``print``(``sum``[``2``:])`

Output

`10001`

Time Complexity: O(n)
Auxiliary Space: O(n)

Example 2:

## Python3

 `# Python program to add two binary numbers.` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``# Declaring the variables``    ``a ``=` `"1101"``    ``b ``=` `"100"``    ` `    ``# Calculating binary sum by using bin() and int()``    ``binary_sum ``=` `lambda` `a,b : ``bin``(``int``(a, ``2``) ``+` `int``(b, ``2``))``    ` `    ``# calling binary_sum lambda function``    ``print``(binary_sum(a,b)[``2``:])``    ` `    ``# This code is contributed by AnkThon`

Output

`10001`

Time complexity :  O(1)

Space Complexity : O(1)

## Python3

 `from` `operator ``import``*``num1``=``"1101"``num2``=``"100"``print``(``bin``(add(``int``(num1,``2``),``int``(num2,``2``))))`

Output

`0b10001`

Time complexity :  O(n)

Space Complexity : O(1)

My Personal Notes arrow_drop_up