Python program to implement Half Subtractor Last Updated : 05 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 outputs. This circuit is used to subtract two single bit binary numbers A and B. The difference and borrow are the two output states of the half subtractor.Examples:Input: A=0; B=1Output: Difference: 1 Borrow: 1Explanation: According to logical expression Difference=A XOR B i.e 0 XOR 1 =1, Borrow=Ā AND B i.e 1 AND 1 =1Input: A=1; B=1Output: Difference: 0 Borrow: 1Logical Expression:Difference = A XOR BBorrow = Ā AND BLogical Diagram:Truth Table:Approach:We take two inputs A and B.XOR operation on A and B gives the value of the Difference.AND operation on Ā and B gives the value of Borrow.Implementation: Python # Python program to implement Half subtractor # Function to print Difference and Borrow def getResult(A, B): # Calculating value of Difference Difference = A ^ B # Calculating value of Borrow # calculating not of A A = not(A) Borrow = A & B # printing the values print("Difference:", Difference) print("Borrow:", Borrow) # Driver code # Inputs A ,B A = 0 B = 1 # passing two inputs of halfadder # as arguments to get result function getResult(A, B) Output:Difference: 1Borrow: 1Method#2: List comprehension:Algorithm:Assign values to A and B.Calculate the difference and borrow using XOR and NOT-AND operations, respectively.Store the results in the result list.Print the results. Python A = 0 B = 1 #Using List comprehension result = [(A ^ B), int(not(A) and B)] # printing the values print("Difference:", result[0]) print("Borrow:", result[1]) #This code is contributed by Jyothi Pinjala. OutputDifference: 1 Borrow: 1Time Complexity: O(1)The time complexity of this code is constant, as it does not depend on the size of the input. It only performs a few operations to calculate the difference and borrow.Auxiliary Space: O(1)The space complexity of this code is also constant, as it only uses a fixed amount of memory to store the input variables and the result list, which contains two integers. Comment More infoAdvertise with us Next Article Python program to implement Half Subtractor vikkycirus Follow Improve Article Tags : Technical Scripter Python Python Programs Digital Logic Technical Scripter 2020 +1 More Practice Tags : python Similar Reads 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 subtra 3 min read Python Program to Subtract K from each digit Given a list, the task is to write a Python Program to subtract K from each digit, if the element gets below 0, retain 0. Examples: Input : test_list = [2345, 8786, 2478, 8664, 3568, 28], K = 4Output : [1, 4342, 34, 4220, 124, 4]Explanation : In 2345, 4 subtracted from 2 is -2, hence ceiled to 0. He 5 min read Python Program to Split a List into Two Halves Splitting a list into two halves is a common operation that can be useful in many cases, such as when dealing with large datasets or when performing specific algorithms (e.g., merge sort). In this article, we will discuss some of the most common methods for splitting a list into two halves.Using Lis 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 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 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 Convert any Positive Real Number to Binary string Given any Real Number greater than or equal to zero that is passed in as float, print binary representation of entered real number. Examples: Input: 123.5 Output: 1 1 1 1 0 1 1 . 1 Input: 0.25 Output: .01 Mathematical Logic along with steps done in programming: Any real number is divided into two pa 4 min read Python Program to find the Quotient and Remainder of two numbers Given two numbers n and m. The task is to find the quotient and remainder of two numbers by dividing n by m. Examples: Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4 Method 1: Naive approach The naive approach is to find the quotient using the 2 min read Python Program to Swap Two Variables The task of swapping two variables in Python involves exchanging their values without losing any data . For example, if x = 10 and y = 50, after swapping, x becomes 50 and y becomes 10. Using Tuple UnpackingTuple unpacking is the most efficient method as it eliminates the need for extra memory or te 3 min read Python program to convert meters in yards and vice versa Given the distance in either meter or yard, the task here is to generate a Python program that converts distance given in meters to yards and vice versa. Examples: Input: Length in Meter: 245 Length in Yard : 100 Output: 245 Meter in Yard = 267.9344 100 Yards in Meter = 91.4403 Input: Length in Mete 2 min read Like