{"record":{"id":"03b256a3125672c0","repo":"TheAlgorithms/Python","slug":"both-arguments-must-be-non-negative","errorCode":null,"errorMessage":"Both arguments MUST be non-negative!","messagePattern":"Both arguments MUST be non-negative!","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/bitwise_addition_recursive.py","lineNumber":41,"sourceCode":"    >>> bitwise_addition_recursive('4.5', 9)\n    Traceback (most recent call last):\n        ...\n    TypeError: Both arguments MUST be integers!\n    >>> bitwise_addition_recursive(-1, 9)\n    Traceback (most recent call last):\n        ...\n    ValueError: Both arguments MUST be non-negative!\n    >>> bitwise_addition_recursive(1, -9)\n    Traceback (most recent call last):\n        ...\n    ValueError: Both arguments MUST be non-negative!\n    \"\"\"\n\n    if not isinstance(number, int) or not isinstance(other_number, int):\n        raise TypeError(\"Both arguments MUST be integers!\")\n\n    if number < 0 or other_number < 0:\n        raise ValueError(\"Both arguments MUST be non-negative!\")\n\n    bitwise_sum = number ^ other_number\n    carry = number & other_number\n\n    if carry == 0:\n        return bitwise_sum\n\n    return bitwise_addition_recursive(bitwise_sum, carry << 1)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":23,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/bitwise_addition_recursive.py#L23-L56","documentation":"Raised by bitwise_addition_recursive when either argument is negative. The recursive carry loop (sum = a^b, carry = (a&b) << 1) only converges for non-negative integers; with negatives the carry never terminates correctly, so they are rejected up front.","triggerScenarios":"Calling bitwise_addition_recursive(-1, 9) or (1, -9) — either argument negative triggers it.","commonSituations":"Using the XOR-carry trick as a novelty adder on signed arithmetic, deltas, or subtraction results where one side can be negative.","solutions":["Use the built-in + operator for signed addition; this function is inherently unsigned.","Handle signs explicitly: compute on abs() values and reapply the sign, mirroring manual signed addition.","Validate operands are >= 0 before the call and route negatives elsewhere."],"exampleFix":"# before\nbitwise_addition_recursive(-1, 9)  # ValueError\n\n# after\nif a < 0 or b < 0:\n    result = a + b\nelse:\n    result = bitwise_addition_recursive(a, b)","handlingStrategy":"validation","validationCode":"if number < 0 or other_number < 0:\n    raise ValueError(\"operands must be non-negative for bitwise addition\")","typeGuard":null,"tryCatchPattern":"try:\n    total = bitwise_addition_recursive(a, b)\nexcept ValueError:\n    total = a + b  # fall back to native addition for signed input","preventionTips":["Check signs before calling; the XOR-carry trick is unsigned-only.","Compute on magnitudes and reapply sign if you must use this adder.","Property-test wrappers with negative operands so sign bugs surface early."],"tags":["bit-manipulation","input-validation","recursion","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}