-
-
Notifications
You must be signed in to change notification settings - Fork 49.3k
Full Adder Implementation in Boolean Algebra #13914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ranjita1708
wants to merge
3
commits into
TheAlgorithms:master
Choose a base branch
from
Ranjita1708:feat/full-adder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,3 +108,4 @@ venv.bak/ | |
| .try | ||
| .vscode/ | ||
| .vs/ | ||
| scripts/python3.bat | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(input_a: int, input_b: int, carry_in: int) -> tuple[int, int]: | ||
| """ | ||
| Compute the sum and carry-out for a Full Adder. | ||
|
|
||
| Args: | ||
| input_a: First input bit (0 or 1). | ||
| input_b: Second input bit (0 or 1). | ||
| carry_in: 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 input_a not in (0, 1) or input_b not in (0, 1) or carry_in not in (0, 1): | ||
| raise ValueError("Inputs must be 0 or 1.") | ||
|
|
||
| # Sum is XOR of the inputs | ||
| sum_bit = input_a ^ input_b ^ carry_in | ||
|
|
||
| # Carry-out is true if any two or more inputs are 1 | ||
| carry_out = (input_a & input_b) | (input_b & carry_in) | (input_a & carry_in) | ||
|
|
||
| return sum_bit, carry_out | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
| """ | ||
| 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() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
aPlease provide descriptive name for the parameter:
b