Open In App

Python program to implement Half Adder

Last Updated : 11 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:
 

  1. Import the required NumPy library.
  2. Define the function half_adder(A, B) which takes two input bits A and B as arguments.
  3. Calculate the Sum of the two input bits using the NumPy bitwise_xor() function which returns the bitwise XOR of two input arrays.
  4. Calculate the Carry of the two input bits using the NumPy bitwise_and() function which returns the bitwise AND of two input arrays.
  5. Return the calculated Sum and Carry values.
  6. Define the input bits A and B.
  7. Call the half_adder() function with the input bits A and B and store the returned Sum and Carry values.
  8. 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:

  1. Simplicity: The Half Adder is relatively easy to design and implement because of the basic gates that are used namely AND and XOR.
  2. 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.
  3. 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:

  1. 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.
  2. 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.



Next Article

Similar Reads