Python Program to Subtract Two Binary Numbers
Last Updated :
30 Jul, 2024
We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.
Examples:
Input: a = "1110" , b = "0110"
Output: "1000"
Explanation: We can see that when "1110" is subtracted from "0110" then the answer is "1000" i.e, 14 - 6 = 8.
Python Program to Subtract Two Numbers Binary
Below, are the methods of Python programs to subtract two numbers binary in Python:
Subtract Two Numbers Binary Using Naive Approach
In this example, below code defines two functions, binary_to_decimal
and decimal_to_binary
, to convert binary numbers to decimal and vice versa. The program then subtracts two binary numbers, "1110" and "1010," by converting them to decimal, performing the subtraction, and converting the result back to binary
Python
# code
def binary_to_decimal(binary):
decimal = 0
power = len(binary) - 1
for digit in binary:
decimal += int(digit) * (2 ** power)
power -= 1
return decimal
def decimal_to_binary(decimal):
binary = ""
while decimal > 0:
remainder = decimal % 2
binary = str(remainder) + binary
decimal = decimal // 2
return binary
num1 = "1110"
num2 = "0110"
print("num1:", num1, "and num2:", num2)
a = binary_to_decimal(num1)
b = binary_to_decimal(num2)
# The resultant difference of decimal numbers
temp = a - b
# converting the subtraction output back to binary
ans = decimal_to_binary(temp)
print("The subtraction result is:", ans)
Outputnum1: 1110 and num2: 0110
The subtraction result is: 1000
Subtract Two Numbers Binary Using int() Function
In this example, below code subtracts two binary numbers, "1110" and "1010," by converting them to decimal using `int()` and then back to binary with `bin()`. It succinctly demonstrates Python's simplicity in performing binary arithmetic operations.
Python
num1 = "1110"
num2 = "0110"
print("num1:", num1, "and num2:", num2)
ans = int(num1, 2) - int(num2, 2)
print("The subtraction using int() function:", bin(ans)[2:])
Outputnum1: 1110 and num2: 0110
The subtraction using int() function: 1000
Subtract Two Numbers Binary Using sub() Function
In this example, below code utilizes the sub()
function from the operator
module to subtract two binary numbers, "1110" and "1010," after converting them to decimal. It then converts the subtraction result back to binary and displays it.
Python
from operator import *
num1 = "1110"
num2 = "0110"
print("num1:", num1, "and num2:", num2)
ans = sub(int(num1, 2), int(num2, 2))
print("The subtraction using sub() function:", bin(ans)[2:])
Outputnum1: 1110 and num2: 0110
The subtraction using sub() function: 1000
Subtract Two Numbers Binary Using 2's Complement
In this example, below code defines a subtraction function, subtraction
, which utilizes the two's complement representation to subtract two binary numbers. It then subtracts "1110" and "1010" by converting them to decimal, performs the subtraction, and displays the result in binary form
Python
def subtraction(num1, num2):
return num1 + (~num2 + 1)
num1 = "1110"
num2 = "0110"
print("num1:", num1, "and num2:", num2)
ans = subtraction(int(num1, 2), int(num2, 2))
print("The subtraction using 2's Complement:", bin(ans))
Outputnum1: 1110 and num2: 0110
The subtraction using 2's Complement: 0b1000
Conclusion
In conclusion, this Python program demonstrates a straightforward yet insightful approach to performing binary subtraction. By leveraging the principles of borrow and carry-over in a binary context, the program provides a clear example of how Python can be employed to handle binary arithmetic operations efficiently. This not only highlights the language's versatility but also underscores its suitability for tasks involving low-level bitwise manipulation.
Similar Reads
Python program to add two binary numbers
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: 10001Approach: Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum become
4 min read
Subtract Two Numbers in Python
Subtracting two numbers in Python is a basic operation where we take two numeric values and subtract one from the other. For example, given the numbers a = 10 and b = 5, the result of a - b would be 5. Let's explore different methods to do this efficiently.Using Minus Operator (-)This is the most st
2 min read
Python Program to Multiply Two Binary Numbers
Given two binary numbers, and the task is to write a Python program to multiply both numbers. Example: firstnumber = 110 secondnumber = 10 Multiplication Result = 1100 We can multiply two binary numbers in two ways using python, and these are: Using bin() functions andWithout using pre-defined funct
2 min read
Python Program to Find the Gcd of Two Numbers
The task of finding the GCD (Greatest Common Divisor) of two numbers in Python involves determining the largest number that divides both input values without leaving a remainder. For example, if a = 60 and b = 48, the GCD is 12, as 12 is the largest number that divides both 60 and 48 evenly. Using e
2 min read
Python Program To Subtract Two Numbers Represented As Linked Lists
Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no
5 min read
Python program to add two Octal numbers
Given two octal numbers, the task is to write a Python program to compute their sum. Examples: Input: a = "123", b = "456" Output: 601 Input: a = "654", b = "321" Output: 1175Approach: To add two octal values in python we will first convert them into decimal values then add them and then finally aga
2 min read
Python program to implement Half Subtractor
Prerequisite: Half Subtractor in Digital LogicGiven two inputs of Half Adder A, B. The task is to implement the Half Subtractor circuit and Print output i.e Difference and Borrow of two inputs.The half subtractor is also a building block for subtracting two binary numbers. It has two inputs and two
2 min read
Python program to convert Base 4 system to binary number
Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1:
3 min read
Python Program to add two hexadecimal numbers
Given two hexadecimal numbers, write a Python program to compute their sum. Examples: Input: a = "01B", b = "378" Output: 393 Explanation: B (11 in decimal) + 8 = 19 (13 in hex), hence addition bit = 3, carry = 1 1 + 7 + 1 (carry) = 9, hence addition bit = 9, carry = 0 0 + 3 + 0 (carry) = 3, hence a
6 min read
Python3 Program to Rotate bits of a number
Bit Rotation: A rotation (or circular shift) is an operation similar to a shift except that the bits that fall off at one end are put back to the other end. In the left rotation, the bits that fall off at the left end are put back at the right end. In the right rotation, the bits that fall off at th
3 min read