-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add function for xor gate #7588
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6afb94
Add function for xor gate
arjitarora26 859364e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c26e8f4
Add test case for xor functions
arjitarora26 56bf665
Update boolean_algebra/xor_gate.py
arjitarora26 a898a38
Update boolean_algebra/xor_gate.py
arjitarora26 841c421
Split long comment line into two lines
arjitarora26 e77e4dd
88 characters per line
cclauss 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
A XOR Gate is a logic gate in boolean algebra which results to True(1) | ||
if only one of the two inputs is 1, and False(1) if even number | ||
of inputs are 1. | ||
Following is the truth table of a XOR Gate: | ||
------------------------------ | ||
| Input 1 | Input 2 | Output | | ||
------------------------------ | ||
| 0 | 0 | 0 | | ||
| 0 | 1 | 1 | | ||
| 1 | 0 | 1 | | ||
| 1 | 1 | 0 | | ||
------------------------------ | ||
|
||
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ | ||
|
||
""" | ||
|
||
|
||
def xor_gate(input_1: int, input_2: int) -> int: | ||
""" | ||
>>> xor_gate(0, 0) | ||
0 | ||
>>> xor_gate(0, 1) | ||
1 | ||
>>> xor_gate(1, 0) | ||
1 | ||
>>> xor_gate(1, 1) | ||
0 | ||
""" | ||
num_ones = 0 | ||
|
||
if input_1 != 0: | ||
num_ones += 1 | ||
|
||
if input_2 != 0: | ||
num_ones += 1 | ||
|
||
return int(num_ones % 2 != 0) | ||
|
||
|
||
def main() -> None: | ||
arjitarora26 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("Truth Table of XOR Gate:") | ||
print("| Input 1 | Input 2 | Output |") | ||
print(f"| 0 | 0 | {xor_gate(0, 0)} |") | ||
print(f"| 0 | 1 | {xor_gate(0, 1)} |") | ||
print(f"| 1 | 0 | {xor_gate(1, 0)} |") | ||
print(f"| 1 | 1 | {xor_gate(1, 1)} |") | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() |
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.
Uh oh!
There was an error while loading. Please reload this page.