Python Program to Subtract Two Binary Numbers
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:
- Naive Approach
- Using int() function
- Using sub() function
- Using 2's Complement
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
# 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)
Output
num1: 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.
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:])
Output
num1: 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.
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:])
Output
num1: 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
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))
Output
num1: 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.