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

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

Leave a Reply Cancel reply

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

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap