Python Program to implement Full Subtractor
Last Updated :
25 Mar, 2023
Prerequisite –Full Subtractor in Digital Logic
In this, we will discuss the overview of the full subtractor and will implement the full subtractor logic in the python language. Also, we will cover with the help of examples. Let’s discuss it one by one.
Given three inputs of Full Subtractor A, B, Bin. The task is to implement the Full Subtractor circuit and Print output i.e Difference (d) and B-Out of three inputs.
Full Subtractor :
The full Subtractor is a combinational circuit that is used to perform subtraction of three input bits: the minuend, subtrahend, and borrow in. The full Subtractor generates two output bits: the difference and borrow out.
Logical Expression :
Difference = (A XOR B) XOR Bin
Borrow Out = Ā Bin + Ā B + B Bin
Truth Table :
Examples :
Input : 0 1 1
Output : Difference=0, B-Out=1
Explanation : According to logical expression Difference= (A XOR B) XOR Bin i.e (0 XOR 1) XOR 1 =0 ,
B-Out=Ā Bin + Ā B + B Bin i.e 1 AND 1 + 1 AND 1 + 1 AND 1 = 1
Input : 1 0 0
Output : Difference=1, B-Out=0
Approach :
- We take three inputs A, B, and Bin.
- Applying (A XOR B) XOR Bin gives the Value of Difference.
- Applying Ā Bin + Ā B + B Bin gives the value of B-Out.
Below is the implementation :
Python3
def getResult(A, B, Bin ):
Difference = (A ^ B) ^ Bin
A1 = not (A)
B_Out = A1 & Bin | A1 & B | B & Bin
print ( "Difference = " , Difference)
print ( "B-Out = " , B_Out)
A = 0
B = 1
Bin = 1
getResult(A, B, Bin )
|
Output :
Difference = 0
B-Out = 1
Approach#2: Using itertools:
- Define a function named getResult that takes three parameters: A, B, and Bin.
- Create a list of all possible input combinations of A, B, and Bin using the itertools.product() function.
- Find the index of the input combination that matches the given inputs (A, B, Bin) using the index() method of the list.
- Create a list of the corresponding outputs for all possible input combinations. This list is defined as outputs.
- Return the corresponding outputs for the given inputs using the same index obtained in step 3.
- In the main program, set the input values A, B, and Bin.
- Call the getResult() function with the input values and store the output values in the variables diff and b_out.
- Print the values of diff and b_out using the print() function.
Python3
import itertools
def getResult(A, B, Bin ):
inputs = list (itertools.product([ 0 , 1 ], repeat = 3 ))
idx = inputs.index((A, B, Bin ))
outputs = [( 0 , 0 ), ( 1 , 1 ), ( 1 , 1 ), ( 0 , 1 ), ( 1 , 0 ), ( 0 , 1 ), ( 0 , 1 ), ( 1 , 1 )]
return outputs[idx]
A = 0
B = 1
Bin = 1
diff, b_out = getResult(A, B, Bin )
print ( "Difference = " , diff)
print ( "B-Out = " , b_out)
|
Output
Difference = 0
B-Out = 1
The time complexity :O(1), because the function getResult() performs a constant number of operations that do not depend on the input size. Specifically, it creates a list of all possible input combinations, which has a fixed size of 8 (2^3), and then performs a constant number of operations.
The auxiliary space :O(1), because it only creates a list of 8 tuples to store the output values, which is a fixed amount of memory that does not depend on the input size.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...