{"record":{"id":"0572a0d31e9c4f2d","repo":"TheAlgorithms/Python","slug":"the-value-of-both-inputs-must-be-positive-0572a0","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_xor_operator.py","lineNumber":36,"sourceCode":"    >>> binary_xor(0, 255)\n    '0b11111111'\n    >>> binary_xor(256, 256)\n    '0b000000000'\n    >>> binary_xor(0, -1)\n    Traceback (most recent call last):\n        ...\n    ValueError: the value of both inputs must be positive\n    >>> binary_xor(0, 1.1)\n    Traceback (most recent call last):\n        ...\n    TypeError: 'float' object cannot be interpreted as an integer\n    >>> binary_xor(\"0\", \"1\")\n    Traceback (most recent call last):\n        ...\n    TypeError: '<' not supported between instances of 'str' and 'int'\n    \"\"\"\n    if a < 0 or b < 0:\n        raise ValueError(\"the value of both inputs must be positive\")\n\n    a_binary = str(bin(a))[2:]  # remove the leading \"0b\"\n    b_binary = str(bin(b))[2:]  # remove the leading \"0b\"\n\n    max_len = max(len(a_binary), len(b_binary))\n\n    return \"0b\" + \"\".join(\n        str(int(char_a != char_b))\n        for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))\n    )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":18,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/binary_xor_operator.py#L18-L53","documentation":"Raised by binary_xor when either input is negative. The function XORs two numbers by comparing their zero-padded binary strings, which assumes non-negative operands. Zero is accepted despite the 'positive' wording because the guard is `a < 0 or b < 0`.","triggerScenarios":"Calling binary_xor(-1, 1) or any call where at least one argument is a negative int. Floats pass the guard and fail later in bin(); strings fail at the `<` comparison.","commonSituations":"XOR-based checksums or pairwise-cancellation logic fed with signed differences or deltas; passing values from subtraction without abs().","solutions":["Use Python's native a ^ b if you need XOR of possibly-negative ints; this helper is string-based and unsigned-only.","Pass abs() of the operands when only magnitudes matter.","Validate a >= 0 and b >= 0 at the call site before invoking binary_xor."],"exampleFix":"# before\nbinary_xor(delta, mask)  # ValueError when delta < 0\n\n# after\nbinary_xor(abs(delta), abs(mask))","handlingStrategy":"validation","validationCode":"if a < 0 or b < 0:\n    raise ValueError(f\"binary_xor requires non-negative ints, got {a}, {b}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a ^ b directly for possibly-negative operands.","Normalize inputs with abs() when only bit patterns matter.","Validate at data ingest that values destined for bitwise ops are unsigned."],"tags":["bit-manipulation","input-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}