BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Add two binary numbers

Last Updated: March 19, 2018 by Chaitanya Singh | Filed Under: Python Examples

In this guide, we will see how to add two binary numbers in Python.

Program for adding two binary numbers

In the following program, we are using two built-in functions int() and bin().

The int() function converts the given string into an integer number considering the provided base value, in the following example we are converting the string(which is a binary value) into an integer number so we are passing the base value as 2 (binary numbers have base 2, decimals have base value 10).

Once the strings are converted into an integer values then we are adding them and the result is converted back to binary number using the bin() function.

# decimal value 1
num1 = '00001'
# decimal value 17
num2 = '10001'

# sum - decimal value 18
# binary value 10010
sum = bin(int(num1,2) + int(num2,2))
print(sum)

Output:

0b10010

Related Examples

  1. Python program to add two numbers
  2. Python program to check if a number is positive, negative or zero
  3. Python program to convert decimal to binary number
  4. Python program to check if a number is prime
  5. Python program to check if a number is even or odd

Top Related Articles:

  1. Python Program to Print Calendar
  2. Python Program to Convert Celsius To Fahrenheit and Vice Versa
  3. Python Program to Convert Decimal to Hexadecimal
  4. Python Program to Add Digits of a Number
  5. Python Program to Convert Kilometers(km) to Miles(mi.)

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap