Skip to content

Commit a662d96

Browse files
arjitarora26pre-commit-ci[bot]cclauss
authored
Add function for xor gate (TheAlgorithms#7588)
* Add function for xor gate * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add test case for xor functions * Update boolean_algebra/xor_gate.py Co-authored-by: Christian Clauss <[email protected]> * Update boolean_algebra/xor_gate.py Co-authored-by: Christian Clauss <[email protected]> * Split long comment line into two lines * 88 characters per line Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent d407476 commit a662d96

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

boolean_algebra/xor_gate.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
A XOR Gate is a logic gate in boolean algebra which results to 1 (True) if only one of
3+
the two inputs is 1, and 0 (False) if an even number of inputs are 1.
4+
Following is the truth table of a XOR Gate:
5+
------------------------------
6+
| Input 1 | Input 2 | Output |
7+
------------------------------
8+
| 0 | 0 | 0 |
9+
| 0 | 1 | 1 |
10+
| 1 | 0 | 1 |
11+
| 1 | 1 | 0 |
12+
------------------------------
13+
14+
Refer - https://www.geeksforgeeks.org/logic-gates-in-python/
15+
"""
16+
17+
18+
def xor_gate(input_1: int, input_2: int) -> int:
19+
"""
20+
calculate xor of the input values
21+
22+
>>> xor_gate(0, 0)
23+
0
24+
>>> xor_gate(0, 1)
25+
1
26+
>>> xor_gate(1, 0)
27+
1
28+
>>> xor_gate(1, 1)
29+
0
30+
"""
31+
return (input_1, input_2).count(0) % 2
32+
33+
34+
def test_xor_gate() -> None:
35+
"""
36+
Tests the xor_gate function
37+
"""
38+
assert xor_gate(0, 0) == 0
39+
assert xor_gate(0, 1) == 1
40+
assert xor_gate(1, 0) == 1
41+
assert xor_gate(1, 1) == 0
42+
43+
44+
if __name__ == "__main__":
45+
print(xor_gate(0, 0))
46+
print(xor_gate(0, 1))

0 commit comments

Comments
 (0)