-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathhalf_adder.py
More file actions
53 lines (41 loc) · 1.23 KB
/
half_adder.py
File metadata and controls
53 lines (41 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
A Half Adder is a basic combinational circuit in digital logic.
It computes the sum and carry outputs for two input bits.
Truth Table:
-------------------------
| Input A | Input B | Sum | Carry |
-------------------------
| 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
-------------------------
Refer - https://en.wikipedia.org/wiki/Adder_(electronics)#Half_adder
"""
def half_adder(input_a: int, input_b: int) -> tuple[int, int]:
"""
Compute the sum and carry for a Half Adder.
Args:
input_a: First input bit (0 or 1).
input_b: Second input bit (0 or 1).
Returns:
A tuple `(sum_bit, carry_bit)`.
>>> half_adder(0, 0)
(0, 0)
>>> half_adder(0, 1)
(1, 0)
>>> half_adder(1, 0)
(1, 0)
>>> half_adder(1, 1)
(0, 1)
Raises:
ValueError: If inputs are not 0 or 1.
"""
if input_a not in (0, 1) or input_b not in (0, 1):
raise ValueError("Inputs must be 0 or 1")
sum_bit = input_a ^ input_b
carry_bit = input_a & input_b
return sum_bit, carry_bit
if __name__ == "__main__":
import doctest
doctest.testmod()