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 Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read