Skip to content

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 7 commits into from
Oct 25, 2022
Merged
Changes from 3 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
55 changes: 55 additions & 0 deletions boolean_algebra/xor_gate.py
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(0) 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:
"""
calculate xor of the input values

>>> 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 test_xor_gate() -> None:
"""
Tests the xor_gate function
"""
assert xor_gate(0, 0) == 0
assert xor_gate(0, 1) == 1
assert xor_gate(1, 0) == 1
assert xor_gate(1, 1) == 0


if __name__ == "__main__":
print(xor_gate(0, 0))
print(xor_gate(0, 1))