From 6a6c5be681f3b811615881b921473e4a7ddc4d8a Mon Sep 17 00:00:00 2001 From: Ranjita1708 Date: Sun, 16 Nov 2025 20:30:36 +0530 Subject: [PATCH 1/5] feat(boolean_algebra): add half adder Implementation --- boolean_algebra/half_adder.py | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 boolean_algebra/half_adder.py diff --git a/boolean_algebra/half_adder.py b/boolean_algebra/half_adder.py new file mode 100644 index 000000000000..838287757ffc --- /dev/null +++ b/boolean_algebra/half_adder.py @@ -0,0 +1,46 @@ +""" +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(a: int, b: int) -> tuple[int, int]: + """ + Compute the sum and carry for a Half Adder. + + >>> 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 a not in (0, 1) or b not in (0, 1): + raise ValueError("Inputs must be 0 or 1") + + sum_bit = a ^ b + carry_bit = a & b + return sum_bit, carry_bit + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 4f129876b068d34a8eb0f9444aaa43bd38bb2258 Mon Sep 17 00:00:00 2001 From: Ranjita1708 Date: Sun, 16 Nov 2025 22:42:05 +0530 Subject: [PATCH 2/5] feat(boolean_algebra): add full adder implementation --- .gitignore | 1 + boolean_algebra/full_adder.py | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 boolean_algebra/full_adder.py diff --git a/.gitignore b/.gitignore index baea84b8d1f1..c30403ceb2f7 100644 --- a/.gitignore +++ b/.gitignore @@ -108,3 +108,4 @@ venv.bak/ .try .vscode/ .vs/ +scripts/python3.bat \ No newline at end of file diff --git a/boolean_algebra/full_adder.py b/boolean_algebra/full_adder.py new file mode 100644 index 000000000000..24b41f151125 --- /dev/null +++ b/boolean_algebra/full_adder.py @@ -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]: + """ + 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() From 7cbf23e1a677fb8616878d482e27a4ad2c500300 Mon Sep 17 00:00:00 2001 From: Ranjita1708 Date: Sun, 16 Nov 2025 23:29:35 +0530 Subject: [PATCH 3/5] feat(boolean_algebra): add half adder implementation --- boolean_algebra/half_adder.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/boolean_algebra/half_adder.py b/boolean_algebra/half_adder.py index 838287757ffc..e8deb2f24f79 100644 --- a/boolean_algebra/half_adder.py +++ b/boolean_algebra/half_adder.py @@ -16,10 +16,17 @@ """ -def half_adder(a: int, b: int) -> tuple[int, int]: +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) @@ -32,15 +39,15 @@ def half_adder(a: int, b: int) -> tuple[int, int]: Raises: ValueError: If inputs are not 0 or 1. """ - if a not in (0, 1) or b not in (0, 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 = a ^ b - carry_bit = a & b + sum_bit = input_a ^ input_b + carry_bit = input_a & input_b return sum_bit, carry_bit if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file From f8fc53a03e76a4d3593237407134984d13f3d782 Mon Sep 17 00:00:00 2001 From: Ranjita1708 Date: Sun, 16 Nov 2025 23:53:33 +0530 Subject: [PATCH 4/5] feat(boolean_algebra): add half adder implementation --- boolean_algebra/half_adder.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/boolean_algebra/half_adder.py b/boolean_algebra/half_adder.py index e8deb2f24f79..5732c7b2f610 100644 --- a/boolean_algebra/half_adder.py +++ b/boolean_algebra/half_adder.py @@ -49,5 +49,6 @@ def half_adder(input_a: int, input_b: int) -> tuple[int, int]: if __name__ == "__main__": import doctest - - doctest.testmod() \ No newline at end of file + + doctest.testmod() + \ No newline at end of file From 42166b2f9c3dc5517f050d12ed563bfc1fdd57f5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Nov 2025 18:24:07 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/half_adder.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/boolean_algebra/half_adder.py b/boolean_algebra/half_adder.py index 5732c7b2f610..4ee6091889bd 100644 --- a/boolean_algebra/half_adder.py +++ b/boolean_algebra/half_adder.py @@ -49,6 +49,5 @@ def half_adder(input_a: int, input_b: int) -> tuple[int, int]: if __name__ == "__main__": import doctest - + doctest.testmod() - \ No newline at end of file