- Python Basic Programs
- Python Program Examples
- Python Print Hello World
- Python Get Input from User
- Python Add Two Numbers
- Add Subtract Multiply Divide
- Python Check Even or Odd
- Python Check Prime or Not
- Python Check Alphabet or Not
- Python Check Vowel or Not
- Python Check Leap Year or Not
- Check Reverse equal Original
- Check Positive Negative Zero
- Python Check Armstrong or Not
- Python Check Palindrome or Not
- Python Check Perfect Number
- Python Find Reverse of Number
- Python Count Digits in Number
- Python Add Digits of Number
- Sum of First and Last Digits
- Python Product of Mid Digits
- Sum of Squares of Digits
- Interchange Digits of Number
- Python Sum of n Numbers
- Python Print ASCII Values
- Python Swap Two Numbers
- Python Swap Two Variables
- Python Fahrenheit to Celsius
- Python Celsius to Fahrenheit
- Python Display Calendar
- Python Days into Years, Weeks
- Find Largest of Two Number
- Find Largest of Three Number
- Python Print Fibonacci Series
- Generate Armstrong Numbers
- Python Make Simple Calculator
- Python Add Binary Numbers
- Binary Number Multiplication
- Python Mathematical Programs
- Find Sum of Natural Numbers
- Find Average of n Numbers
- Python Print Multiplication Table
- Print Table using Recursion
- Python Find Average Percentage
- Python Find Grade of Student
- Find Square Root of Number
- Python Print Prime Numbers
- Find Numbers Divisible by
- Python Find Factors of Number
- Python Find Factorial of a Number
- Python Find HCF & LCM
- Python Kilometres to Miles
- Python Find Area of Square
- Python Find Area of Rectangle
- Python Find Area of Triangle
- Python Find Area of Circle
- Python Find Perimeter of Square
- Find Perimeter of Rectangle
- Python Find Perimeter of Triangle
- Find Circumference of Circle
- Python Simple Interest
- Python Solve Quadratic Equation
- Python Different Set of Operations
- Python Display Powers of 2
- Python Find nCr & nPr
- Python Pattern Programs
- Python Print Pattern Programs
- Python Print Diamond Pattern
- Python Print Floyd's Triangle
- Python Print Pascal's Triangle
- Python List Programs
- Python Count Even/Odd in List
- Python Positive/Negative in List
- Python Even Numbers in List
- Python Odd Numbers in List
- Python Sum of Elements in List
- Sum of Odd/Even Numbers
- Python Element at Even Position
- Python Element at Odd Position
- Python Search Element in List
- Python Largest Number in List
- Python Smallest Number in List
- Python Second Largest in List
- Python Second Smallest in List
- Python Insert Element in List
- Python Delete Element from List
- Python Multiply Numbers in List
- Swap Two Elements in List
- Python 1D Array Program
- Python Linear Search
- Python Binary Search
- Python Insertion Sort
- Python Bubble Sort
- Python Selection Sort
- Remove Duplicates from List
- Python Reverse a List
- Python Merge Two List
- Python Copy a List
- Python Conversion Programs
- Python Decimal to Binary
- Python Decimal to Octal
- Python Decimal to Hexadecimal
- Python Binary to Decimal
- Python Binary to Octal
- Python Binary to Hexadecimal
- Python Octal to Decimal
- Python Octal to Binary
- Python Octal to Hexadecimal
- Python Hexadecimal to Decimal
- Python Hexadecimal to Binary
- Python Hexadecimal to Octal
- Python Matrix Programs
- Python Add Two Matrices
- Python Subtract Two Matrices
- Python Transpose Matrix
- Python Multiply Matrices
- Python String Programs
- Python Print String
- Python Find Length of String
- Python Compare Two Strings
- Python Copy String
- Python Concatenate String
- Python Reverse a String
- Python Swap Two Strings
- Python Uppercase to Lowercase
- Python Lowercase to Uppercase
- Python Check Substring in String
- Python Count Character in String
- Count Repeated Characters
- Python Count Word in Sentence
- Python Count Each Vowels
- Python Capitalize Character
- Python Capitalize Word in String
- Python Smallest/Largest Word
- Remove Spaces from String
- Remove Duplicate Character
- Remove Vowels from String
- Remove Punctuation from String
- Python Remove Word in String
- Python Remove Duplicate Words
- WhiteSpace to Hyphens
- Replace Vowels with Character
- Replace Character in String
- Python Sort String in Alphabetical
- Sort Word in Alphabetical Order
- Extract Number from String
- Python Check Anagram Strings
- Python File Programs
- Python Read a File
- Python Write to File
- Python Append Text to File
- Python Copy Files
- Python Merge Two Files
- Python Counts Characters in File
- Python Count Words in File
- Python File Content in Reverse
- Python Lines Contains String
- Python Delete Line from File
- Python Capitalize Word in File
- Python Replace Text in File
- Replace Specific Line in File
- Python Find Size of File
- Python List Files in Directory
- Python Delete Files
- Python Misc Programs
- Python Reverse a Tuple
- Python Merge Two Dictionary
- Python bytes to String
- Python bytearray to String
- Generate Random Numbers
- Python Print Address of Variable
- Python Print Date and Time
- Python Get IP Address
- Python Shutdown/Restart PC
- Python Tutorial
- Python Tutorial

# Python Program to Add Two Binary Numbers

In this article, you will learn and get code to add two binary numbers entered by user using a Python program. Here are the list of programs on binary number addition:

- Add Two Binary Numbers Directly
- Add Two Binary Numbers using User-based Code

### How to Add Two Binary Numbers ?

Here are the rules for binary number addition:

1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 1 + 1 = 10 (0 and carry 1) 1 + 1 + 1 = 11 (1 and carry 1)

For example

1 1 1 0 1 + 1 1 1 1 1 ----------- 1 1 1 1 0 0

## Add Two Binary Numbers Directly

This program find and prints the sum of two given binary numbers in a direct way. Here direct way means, this program is created using
**int()** and **bin()**, pre-defined function of Python. Let's have a look at the program. I'll explain it later on:

print("Enter First Binary Number: ") nOne = int(input()) print("Enter Second Binary Number: ") nTwo = int(input()) nOne = str(nOne) nTwo = str(nTwo) iSum = int(nOne, 2) + int(nTwo, 2) bSum = bin(iSum) print("Result = " + bSum)

Here is its sample run:

Now supply any two binary numbers say **1110** as first and **1111** as second binary number. Here is the
sample run with exactly same input:

That is, **1110 + 1111 = 11101** or **0b1110 + 0b1111 = 0b11101**.

**Note - **The **int(num, 2)** returns a decimal integer equivalent of binary number stored in **num**. Here
**2** is the base of number stored in **num**

Therefore the following statement:

iSum = int(nOne, 2) + int(nTwo, 2)

converts **nOne** and **nTwo** into its decimal integer equivalent and adds them. Its addition result gets
initialized to **iSum** variable. And using **bin()** method, we've converted the decimal integer value into
its equivalent binary number.

**Note - **The **bin()** returns binary equivalent string of an integer passed as its argument.

The dry run of above program with user input **1110** and **1111** goes like:

- When user enters these two binary numbers, it gets stored in
**nOne**and**nTwo**respectively. So**nOne=1110**and**nTwo=1111** - Now using
**nOne = str(nOne)**,**nOne**'s value becomes a string value. That is, the type of**nOne**gets changed to a string type. Similar thing happened with**nTwo** - Then using
**iSum = int(nOne, 2) + int(nTwo, 2)**

**int(nOne, 2) + int(nTwo, 2)**or**14 + 15**or**29**gets initialized to**iSum**. As already told that,**int(nOne, 2)**returns a decimal integer equivalent of value stored in**nOne** - Finally using
**bin(iSum)**, the value**29**gets converted into binary equivalent string, that is**0b11101**.**11101**is binary equivalent of**29** - Now just print the value of
**iSum**as addition result of two entered binary numbers

#### Modified Version of Previous Program

This program is the modified version of previous program with some better looking output:

print(end="Enter First Binary Number: ") nOne = int(input()) print(end="Enter Second Binary Number: ") nTwo = int(input()) nOne = str(nOne) nTwo = str(nTwo) iSum = int(nOne, 2) + int(nTwo, 2) bSum = bin(iSum) print("\n" + nOne + " + " + nTwo + " = " + bSum[2:])

Here is its sample run with user input, **111** as first and **111** as second binary number:

**Note - **The **bSum[2:]** is used to print from **2 ^{nd}** index. It is used to skip

**0b**of

**bSum**. That is

**0**is at

**0**and

^{th}**b**is at

**1**index.

^{st}## Add Two Binary Numbers using User-Defined Code

This program is created with complete user-based code to add two binary numbers entered by user. Because in this program, we've not used any type of pre-defined function:

print(end="Enter First Binary Number: ") nOne = int(input()) print(end="Enter Second Binary Number: ") nTwo = int(input()) nOne = str(nOne) nTwo = str(nTwo) mLen = max(len(nOne), len(nTwo)) nOne = nOne.zfill(mLen) nTwo = nTwo.zfill(mLen) addRes = "" carry = 0 for i in range(mLen - 1, -1, -1): re = carry if nOne[i] == '1': re = re+1 else: re = re+0 if nTwo[i] == '1': re = re+1 else: re = re+0 if re%2==1: addRes = '1' + addRes else: addRes = '0' + addRes if re<2: carry = 0 else: carry = 1 if carry!=0: addRes = '1' + addRes print(addRes)

Here is its sample run, **11101** as first and **11111** as second binary number:

**Note - **The **zfill()** method is used to add zeros (0) at beginning of the string. Its length is specified through its argument.

**Note - **The **max()** returns the maximum number from numbers provided as its argument.

#### Modified Version of Previous Program

This program is again the modified version of previous program. In this program, we've shorten the logical code to create the program that looks little smaller than previous one:

print(end="Enter First Binary Number: ") nOne = int(input()) print(end="Enter Second Binary Number: ") nTwo = int(input()) nOne = str(nOne) nTwo = str(nTwo) mLen = max(len(nOne), len(nTwo)) nOne = nOne.zfill(mLen) nTwo = nTwo.zfill(mLen) addRes = "" carry = 0 for i in range(mLen - 1, -1, -1): re = carry re += 1 if nOne[i] == '1' else 0 re += 1 if nTwo[i] == '1' else 0 addRes = ('1' if re%2==1 else '0') + addRes carry = 0 if re<2 else 1 if carry!=0: addRes = '1' + addRes print("\n" + nOne + " + " + nTwo + " = " + addRes)

Here is its sample run with user input **101** as first and **100** as second binary number:

« Previous Program Next Program »