Open In App

Python Program to implement Full Subtractor

Last Updated : 25 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# python program to implement full Subtractor
# Function to print Difference and B-Out
 
def getResult(A, B, Bin):
 
    # Calculating value of Difference
    Difference = (A ^ B) ^ Bin
     
    # calculating NOT  value of a
    A1 = not(A)
     
    # Calculating value of B-Out
    B_Out = A1 & Bin | A1 & B | B & Bin
 
    # printing the values
    print("Difference = ", Difference)
    print("B-Out = ", B_Out)
 
 
# Driver code
A = 0
B = 1
Bin = 1
# passing three inputs of fullsubtractor as arguments to get result function
getResult(A, B, Bin)


Output :

Difference =  0
B-Out      =  1

Approach#2: Using itertools:

  1. Define a function named getResult that takes three parameters: A, B, and Bin.
  2. Create a list of all possible input combinations of A, B, and Bin using the itertools.product() function.
  3. Find the index of the input combination that matches the given inputs (A, B, Bin) using the index() method of the list.
  4. Create a list of the corresponding outputs for all possible input combinations. This list is defined as outputs.
  5. Return the corresponding outputs for the given inputs using the same index obtained in step 3.
  6. In the main program, set the input values A, B, and Bin.
  7. Call the getResult() function with the input values and store the output values in the variables diff and b_out.
  8. Print the values of diff and b_out using the print() function.

Python3




import itertools
def getResult(A, B, Bin):
    # create a list of all possible input combinations
    inputs = list(itertools.product([0, 1], repeat=3))
    # find the input combination that matches the given inputs
    idx = inputs.index((A, B, Bin))
    # use the same index to get the corresponding outputs
    outputs = [(0, 0), (1, 1), (1, 1), (0, 1), (1, 0), (0, 1), (0, 1), (1, 1)]
    return outputs[idx]
# Driver code
A = 0
B = 1
Bin = 1
# passing three inputs of fullsubtractor as arguments to get result function
diff, b_out = getResult(A, B, Bin)
print("Difference = ", diff)
print("B-Out = ", b_out)
#This code is contributed by Jyothi Pinjala.


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
Previous
Next
Share your thoughts in the comments

Similar Reads