Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ venv.bak/
.try
.vscode/
.vs/
scripts/python3.bat
67 changes: 67 additions & 0 deletions boolean_algebra/full_adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
A Full Adder is a fundamental combinational circuit in digital logic.
It computes the sum and carry outputs for two input bits and an input carry bit.

Truth Table:
-----------------------------------------
| A | B | Cin | Sum | Cout |
-----------------------------------------
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
-----------------------------------------

Refer:
https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder
"""


def full_adder(a: int, b: int, cin: int) -> tuple[int, int]:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

"""
Compute the sum and carry-out for a Full Adder.

Args:
a: First input bit (0 or 1).
b: Second input bit (0 or 1).
cin: Carry-in bit (0 or 1).

Returns:
A tuple `(sum_bit, carry_out)`.

>>> full_adder(0, 0, 0)
(0, 0)
>>> full_adder(0, 1, 0)
(1, 0)
>>> full_adder(1, 0, 0)
(1, 0)
>>> full_adder(1, 1, 0)
(0, 1)
>>> full_adder(0, 0, 1)
(1, 0)
>>> full_adder(1, 1, 1)
(1, 1)

Raises:
ValueError: If any input is not 0 or 1.
"""
if a not in (0, 1) or b not in (0, 1) or cin not in (0, 1):
raise ValueError("Inputs must be 0 or 1.")

# Sum is XOR of the inputs
sum_bit = a ^ b ^ cin

# Carry-out is true if any two or more inputs are 1
carry_out = (a & b) | (b & cin) | (a & cin)

return sum_bit, carry_out


if __name__ == "__main__":
import doctest

doctest.testmod()
54 changes: 54 additions & 0 deletions boolean_algebra/half_adder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
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

Check failure on line 52 in boolean_algebra/half_adder.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

boolean_algebra/half_adder.py:52:1: W293 Blank line contains whitespace
doctest.testmod()

Check failure on line 54 in boolean_algebra/half_adder.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

boolean_algebra/half_adder.py:54:6: W292 No newline at end of file

Check failure on line 54 in boolean_algebra/half_adder.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

boolean_algebra/half_adder.py:54:1: W293 Blank line contains whitespace
Loading