MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / mux

Function mux

boolean_algebra/multiplexer.py:1–36  ·  view source on GitHub ↗

Implement a 2-to-1 Multiplexer. :param input0: The first input value (0 or 1). :param input1: The second input value (0 or 1). :param select: The select signal (0 or 1) to choose between input0 and input1. :return: The output based on the select signal. input1 if select else i

(input0: int, input1: int, select: int)

Source from the content-addressed store, hash-verified

1def mux(input0: int, input1: int, select: int) -> int:
2 """
3 Implement a 2-to-1 Multiplexer.
4
5 :param input0: The first input value (0 or 1).
6 :param input1: The second input value (0 or 1).
7 :param select: The select signal (0 or 1) to choose between input0 and input1.
8 :return: The output based on the select signal. input1 if select else input0.
9
10 https://www.electrically4u.com/solved-problems-on-multiplexer
11 https://en.wikipedia.org/wiki/Multiplexer
12
13 >>> mux(0, 1, 0)
14 0
15 >>> mux(0, 1, 1)
16 1
17 >>> mux(1, 0, 0)
18 1
19 >>> mux(1, 0, 1)
20 0
21 >>> mux(2, 1, 0)
22 Traceback (most recent call last):
23 ...
24 ValueError: Inputs and select signal must be 0 or 1
25 >>> mux(0, -1, 0)
26 Traceback (most recent call last):
27 ...
28 ValueError: Inputs and select signal must be 0 or 1
29 >>> mux(0, 1, 1.1)
30 Traceback (most recent call last):
31 ...
32 ValueError: Inputs and select signal must be 0 or 1
33 """
34 if all(i in (0, 1) for i in (input0, input1, select)):
35 return input1 if select else input0
36 raise ValueError("Inputs and select signal must be 0 or 1")
37
38
39if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected