Python program to implement Half Adder
Last Updated :
11 Sep, 2024
Prerequisite: Half Adder in Digital Logic
Given two inputs of Half Adder A, B. The task is to implement the Half Adder circuit and Print output i.e sum and carry of two inputs.
Half Adder: A half adder is a type of adder, an electronic circuit that performs the addition of numbers. The half adder is able to add two single binary digits and provide the output plus a carrying value. It has two inputs, called A and B, and two outputs S (sum) and C (carry).

Logical Expression:
Sum = A XOR B
Carry = A AND B
Truth Table:

Examples:
Input : 0 1
Output: Sum=1, Carry=0
Explanation: According to logical expression Sum=A XOR B i.e 0 XOR 1 =1 , Carry=A AND B i.e 0 AND 1 =0
Input : 1 1
Output: Sum=0, Carry=1
Explanation: According to logical expression Sum=A XOR B i.e 1 XOR 1 =0 , Carry=A AND B i.e 1 AND 1 =1
Approach:
- We take two inputs A and B.
- XOR operation on A and B gives the value of the sum.
- AND operation on A and B gives the value of Carry.
Below is the implementation.
Python
# Function to print sum and carry
def getResult(A, B):
# Calculating value of sum
Sum = A ^ B
# Calculating value of Carry
Carry = A & B
# printing the values
print("Sum ", Sum)
print("Carry", Carry)
# Driver code
A = 0
B = 1
# passing two inputs of halfadder as arguments to get result function
getResult(A, B)
Output:
Sum 1
Carry 0
Method#2:Using numpy:
Algorithm:
- Import the required NumPy library.
- Define the function half_adder(A, B) which takes two input bits A and B as arguments.
- Calculate the Sum of the two input bits using the NumPy bitwise_xor() function which returns the bitwise XOR of two input arrays.
- Calculate the Carry of the two input bits using the NumPy bitwise_and() function which returns the bitwise AND of two input arrays.
- Return the calculated Sum and Carry values.
- Define the input bits A and B.
- Call the half_adder() function with the input bits A and B and store the returned Sum and Carry values.
- Print the calculated Sum and Carry values.
Python
import numpy as np
def half_adder(A, B):
Sum = np.bitwise_xor(A, B)
Carry = np.bitwise_and(A, B)
return Sum, Carry
# Driver code
A = 0
B = 1
Sum, Carry = half_adder(A, B)
print("Sum:", Sum)
print("Carry:", Carry)
#This code is contributed by Jyothi Pinjala.
Output:
Sum: 1
Carry: 0
Time complexity:
The time complexity of this code is O(1) since the calculations involved in the half_adder() function take constant time regardless of the input values.
Auxiliary Space:
The space complexity of this code is O(1) since the function only uses a constant amount of memory to store the input variables, intermediate variables, and output variables.
What is a Half Adder ?
Half Adder is a basic building block of todays digital circuits in electronics and computers engineering. It performs the addition of two single-bit binary numbers and produces two outputs: In other words, a Sum, and a Carry. Sum is obtained from addition of the two binary inputs while Carry gives an indication whether there occurred an overflow in the addition process. The Half Adder has two inputs, usually labeled as A and B, and two outputs: S (Sum) & C (Carry).
Logical Expressions:
Sum (S) = A XOR B: The XOR (exclusive OR) operation gives the sum of the inputs.
Carry (C) = A AND B: The AND operation gives the carry of the inputs.
Truth Table:
A
| B
| Sum (S)
| Carry (C)
|
---|
0
|
0
|
0
|
0
|
---|
0
|
1
|
1
|
0
|
---|
1
|
0
|
1
|
0
|
---|
1
|
1
|
0
|
1
|
---|
The table above shows the possible input combinations and their corresponding Sum and Carry values.
Advantages and Disadvantages of Half Adder
Advantages:
- Simplicity: The Half Adder is relatively easy to design and implement because of the basic gates that are used namely AND and XOR.
- Speed: Due to the fact that it handles only single bit addition, the Half Adder is fast and thus is suitable for use in low order arithmetic calculations.
- Foundation for Complex Circuits: Half Adders are actually sub circuits in the more elaborate circuits such as the Full Adders to handle multi bit binary numbers.
Disadvantages:
- Limited Functionality: The Half Adder is employed to add two single bit numbers only and does not take into consideration carry-in values which are often required in multi-bit addition.
- Not Suitable for Large Calculations: For adding larger numbers, multiple Half Adders are required with other logic circuits which makes the circuit more complex and complicated.
Conclusion
The Half Adder is an important component used in the digital electronics particularly in the circuits that are used in arithmetic. It gives a basic form of addition for two single bit number and a foundation for binary addition which is the base of other computational methods. However, its main drawback is the inability to manage the carry-over from previous stages, but, all the same, the simplicity and ease of operation of the Half Adder are useful in different contexts.
Similar Reads
Python program to implement Full Adder
Prerequisite : Full Adder in Digital LogicGiven three inputs of Full Adder A, B,C-IN. The task is to implement the Full Adder circuit and Print output i.e sum and C-Out of three inputs. Full Adder : A Full Adder is a logical circuit that performs an addition operation on three one-bit binary numbers
2 min read
C++ program to implement Half Adder
Prerequisite : Half Adder in Digital Logic We are given with two inputs A and B. Our task is to implement Half Adder circuit and print the outputs sum and carry of two inputs. Introduction : The Half adder is a combinational circuit which add two1-bit binary numbers which are augend and addend to gi
2 min read
Python Program to implement Full Subtractor
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
4 min read
Python program to implement 2:4 Multiplexer
Prerequisite : Multiplexers in Digital Logic Introduction : It is a combinational circuit which have many data inputs and single output depending on control or select inputs.â For N input lines, log n (base2) selection lines, or we can say that for 2n input lines, n selection lines are required. Mul
3 min read
C++ program to implement full subtractor
Prerequisite : Full SubtractorGiven three inputs of Full Subtractor A, B, Bin. The task is to implement the Full Subtractor circuit and Print output states difference (D) and Bout of three inputs. Introduction :The full subtractor is used to subtract three 1-bit numbers which are minuend, subtrahend
2 min read
Implementation of Full Adder Using Half Adders
Implementation of full adder from half adders is possible because the half adders add two 1-bit inputs in full adder we add three 1-bit inputs. To obtain a full adder from a half adder we take the first two inputs and add them and use the sum and carry outputs and the third input to get the final su
5 min read
Python | Adding two list elements
Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Python | Decimal as_integer_ratio() method
Decimal#as_integer_ratio() : as_integer_ratio() is a Decimal class method which returns the exponent after shifting out the coefficientâs rightmost digits until only the lead digit remains : as a pair (n, d) Syntax: Decimal.as_integer_ratio() Parameter: Decimal values Return: the exponent after shif
2 min read
Last Minute Notes (LMNs) - Python Programming
Python is a widely-used programming language, celebrated for its simplicity, comprehensive features, and extensive library support. This "Last Minute Notes" article aims to offer a quick, concise overview of essential Python topics, including data types, operators, control flow statements, functions
15+ min read
How to perform multiplication using CherryPy in Python?
CherryPy also known as a web application library is a Python web framework that provides a friendly interface to the HTTP protocol for Python developers. It allows developers to build web applications the same way as in traditional object-oriented Python programs. Thereby, resulting in smaller sourc
2 min read