{"record":{"id":"6be8c70ae8f44342","repo":"TheAlgorithms/Python","slug":"inputs-and-select-signal-must-be-0-or-1","errorCode":null,"errorMessage":"Inputs and select signal must be 0 or 1","messagePattern":"Inputs and select signal must be 0 or 1","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"boolean_algebra/multiplexer.py","lineNumber":36,"sourceCode":"    1\n    >>> mux(1, 0, 1)\n    0\n    >>> mux(2, 1, 0)\n    Traceback (most recent call last):\n        ...\n    ValueError: Inputs and select signal must be 0 or 1\n    >>> mux(0, -1, 0)\n    Traceback (most recent call last):\n        ...\n    ValueError: Inputs and select signal must be 0 or 1\n    >>> mux(0, 1, 1.1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Inputs and select signal must be 0 or 1\n    \"\"\"\n    if all(i in (0, 1) for i in (input0, input1, select)):\n        return input1 if select else input0\n    raise ValueError(\"Inputs and select signal must be 0 or 1\")\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":18,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/boolean_algebra/multiplexer.py#L18-L43","documentation":"Raised by mux() when any of input0, input1, or select is not exactly the int 0 or 1. The multiplexer models a hardware 2-to-1 MUX, so all three signals must be strict binary; the check uses membership `i in (0, 1)` which also rejects 1.0, True-adjacent floats, and any other value.","triggerScenarios":"Calling mux(0, 2, 1), mux(0, -1, 0), or mux(0, 1, 1.1) as in the doctests. Also mux(True, 1, 0) passes (bool == int 1) but mux(0, 1, 0.0) fails because 0.0 in (0, 1) is True — careful: 0.0 == 0 so floats 0.0/1.0 actually pass; values like 2, -1, 1.1 fail.","commonSituations":"Feeding unnormalized boolean data (e.g. -1/1 encoding, probabilities, or numpy ints that are fine but floats like 1.5 that are not), or passing results of arithmetic that can exceed 1.","solutions":["Normalize signals to binary before calling: s = int(bool(s)).","Validate all three arguments against (0, 1) in your caller and reject/clip earlier.","If using -1/1 encoding, map to 0/1 first: (x + 1) // 2."],"exampleFix":"# before\nmux(0, 1, 0.7)  # ValueError: Inputs and select signal must be 0 or 1\n\n# after\nmux(0, 1, int(select > 0.5))  # binary select","handlingStrategy":"validation","validationCode":"if not all(i in (0, 1) for i in (input0, input1, select)):\n    raise ValueError('mux signals must be binary 0/1')","typeGuard":"def is_binary(v: object) -> bool:\n    return isinstance(v, int) and v in (0, 1)","tryCatchPattern":null,"preventionTips":["Normalize booleans with int(bool(x)) before hardware-style gates.","Map -1/1 encodings to 0/1 explicitly."],"tags":["boolean-algebra","multiplexer","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}