Skip to content

Commit cc63532

Browse files
authored
Create multiplexer.py
1 parent e3eb9da commit cc63532

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Diff for: boolean_algebra/multiplexer.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://www.electrically4u.com/solved-problems-on-multiplexer/
2+
3+
def mux(input0: int, input1: int, select: int) -> int:
4+
"""
5+
Implement a 2-to-1 Multiplexer.
6+
7+
:param input0: The first input value (0 or 1).
8+
:param input1: The second input value (0 or 1).
9+
:param select: The select signal (0 or 1) to choose between input0 and input1.
10+
:return: The output based on the select signal.
11+
12+
>>> mux(0, 1, 0)
13+
0
14+
>>> mux(0, 1, 1)
15+
1
16+
>>> mux(1, 0, 0)
17+
1
18+
>>> mux(1, 0, 1)
19+
0
20+
"""
21+
if select == 0:
22+
return input0
23+
elif select == 1:
24+
return input1
25+
else:
26+
raise ValueError("Select signal must be 0 or 1")
27+
28+
if __name__ == "__main__":
29+
import doctest
30+
31+
doctest.testmod()

0 commit comments

Comments
 (0)