{"record":{"id":"ddc47bcf89f37a4b","repo":"TheAlgorithms/Python","slug":"both-arguments-must-be-integers","errorCode":null,"errorMessage":"Both arguments MUST be integers!","messagePattern":"Both arguments MUST be integers!","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/bitwise_addition_recursive.py","lineNumber":38,"sourceCode":"    Traceback (most recent call last):\n        ...\n    TypeError: Both arguments MUST be integers!\n    >>> 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":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/bitwise_addition_recursive.py#L20-L56","documentation":"Raised by bitwise_addition_recursive when either argument is not an int. The algorithm computes the sum purely with ^, &, and << operators, so it requires true integers; floats and strings are rejected before any bit operation runs. Note that bool passes this check because bool subclasses int.","triggerScenarios":"Calling bitwise_addition_recursive(1.5, 2) or ('1', 2). Values that are ints pass through; True/False are accepted since isinstance(True, int) is True.","commonSituations":"Feeding parsed numeric strings or float results from math modules into a bitwise adder; mixing types from JSON/dynamic data without coercion.","solutions":["Convert operands to int at the call site: bitwise_addition_recursive(int(a), int(b)).","For ordinary addition just use a + b; keep this function for learning/benchmark scenarios where operands are already ints.","If strictness beyond bool is needed, add `isinstance(x, bool)` exclusion before calling."],"exampleFix":"# before\nbitwise_addition_recursive(2.0, 3)  # TypeError\n\n# after\nbitwise_addition_recursive(int(2.0), 3)  # 5","handlingStrategy":"type-guard","validationCode":"if not all(isinstance(x, int) and not isinstance(x, bool) for x in (number, other_number)):\n    raise TypeError(\"bitwise addition needs true ints\")","typeGuard":"def are_ints(*values: object) -> bool:\n    return all(isinstance(v, int) for v in values)","tryCatchPattern":"try:\n    total = bitwise_addition_recursive(a, b)\nexcept TypeError:\n    total = int(a) + int(b)","preventionTips":["Coerce with int() where values come from parsing or math modules.","Remember bool passes the check — exclude it explicitly if needed.","For production code prefer the + operator; keep this for teaching/benchmarks."],"tags":["bit-manipulation","type-validation","recursion","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}