{"record":{"id":"a4f2f77bcc04dc58","repo":"TheAlgorithms/Python","slug":"the-value-of-both-inputs-must-be-positive-a4f2f7","errorCode":null,"errorMessage":"the value of both inputs must be positive","messagePattern":"the value of both inputs must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/binary_or_operator.py","lineNumber":35,"sourceCode":"    >>> binary_or(0, 255)\r\n    '0b11111111'\r\n    >>> binary_or(0, 256)\r\n    '0b100000000'\r\n    >>> binary_or(0, -1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: the value of both inputs must be positive\r\n    >>> binary_or(0, 1.1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    TypeError: 'float' object cannot be interpreted as an integer\r\n    >>> binary_or(\"0\", \"1\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    TypeError: '<' not supported between instances of 'str' and 'int'\r\n    \"\"\"\r\n    if a < 0 or b < 0:\r\n        raise ValueError(\"the value of both inputs must be positive\")\r\n    a_binary = str(bin(a))[2:]  # remove the leading \"0b\"\r\n    b_binary = str(bin(b))[2:]\r\n    max_len = max(len(a_binary), len(b_binary))\r\n    return \"0b\" + \"\".join(\r\n        str(int(\"1\" in (char_a, char_b)))\r\n        for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))\r\n    )\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n","sourceCodeStart":17,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/binary_or_operator.py#L17-L49","documentation":"Raised by binary_or when either input is negative. The function builds the OR of two numbers from their binary string representations, which assumes non-negative values. Despite the 'must be positive' wording, 0 is accepted because the guard is `a < 0 or b < 0`.","triggerScenarios":"Calling binary_or(-1, 1), binary_or(3, -2), or any call where at least one argument is a negative int. Floats like (0, 1.1) pass this guard and fail later with TypeError from bin(); strings fail at the `<` comparison.","commonSituations":"Feeding signed integers from parsers, offsets, deltas, or subtraction results into a function that assumes unsigned bit patterns.","solutions":["Pass abs(a) / abs(b) if the sign is irrelevant to your bit logic.","Fix the caller to clamp or validate the value range before invoking binary_or.","If negative numbers must be supported, use Python's native a | b and format with bin() instead of this helper."],"exampleFix":"# before\nbinary_or(-3, 5)  # ValueError\n\n# after\nbinary_or(abs(-3), 5)  # '0b111'","handlingStrategy":"validation","validationCode":"if a < 0 or b < 0:\n    raise ValueError(f\"binary_or requires non-negative ints, got {a}, {b}\")","typeGuard":null,"tryCatchPattern":"try:\n    result = binary_or(a, b)\nexcept ValueError:\n    result = binary_or(abs(a), abs(b))  # only if sign is truly irrelevant","preventionTips":["Apply abs() to deltas/offsets before bitwise helpers.","Treat all bit_manipulation helpers as unsigned-only APIs.","Prefer the native a | b operator when signs can be negative."],"tags":["bit-manipulation","input-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}