{"record":{"id":"17fecc5b1fa2ed26","repo":"TheAlgorithms/Python","slug":"the-value-of-input-must-be-non-negative","errorCode":null,"errorMessage":"The value of input must be non-negative","messagePattern":"The value of input must be non-negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/reverse_bits.py","lineNumber":67,"sourceCode":"    >>> reverse_bit(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: The value of input must be non-negative\n\n    >>> reverse_bit(1.1)\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value must be an 'int' type\n\n    >>> reverse_bit(\"0\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value must be an 'int' type\n    \"\"\"\n    if not isinstance(number, int):\n        raise TypeError(\"Input value must be an 'int' type\")\n    if number < 0:\n        raise ValueError(\"The value of input must be non-negative\")\n\n    result = 0\n    # iterator over [0 to 31], since we are dealing with a 32 bit integer\n    for _ in range(32):\n        # left shift the bits by unity\n        result <<= 1\n        # get the end bit\n        end_bit = number & 1\n        # right shift the bits by unity\n        number >>= 1\n        # add that bit to our answer\n        result |= end_bit\n    return result\n\n\nif __name__ == \"__main__\":\n    import doctest\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/reverse_bits.py#L49-L85","documentation":"Raised by reverse_bit() when the argument is an int but negative. The algorithm iterates exactly 32 bit positions assuming an unsigned 32-bit value; negative ints in Python have infinite sign-extended two's-complement bits, so reversing them is undefined for this API and rejected.","triggerScenarios":"Calling reverse_bit(-1) or any negative int. Note this check runs after the isinstance check, so reverse_bit(-1.0) raises TypeError first.","commonSituations":"Computing deltas or offsets that can go negative, converting from C uint32 code that assumed wrapping, or sign mishandling when parsing two's-complement hex.","solutions":["Pass the unsigned 32-bit equivalent: reverse_bit(value & 0xFFFFFFFF).","Validate range upstream: assert 0 <= number < 2**32 before calling.","Re-express the algorithm (e.g. reverse within signed width) if negative inputs are genuinely meaningful."],"exampleFix":"# before\nreverse_bit(-3)  # ValueError: The value of input must be non-negative\n\n# after\nreverse_bit(-3 & 0xFFFFFFFF)  # reverses the 32-bit two's-complement pattern","handlingStrategy":"validation","validationCode":"if number < 0:\n    number &= 0xFFFFFFFF  # reinterpret as unsigned 32-bit\nassert 0 <= number < 2**32","typeGuard":"def is_uint32(n: object) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and 0 <= n < 2**32","tryCatchPattern":"try:\n    reverse_bit(n)\nexcept ValueError as e:\n    if 'non-negative' in str(e):\n        n &= 0xFFFFFFFF\n        result = reverse_bit(n)\n    else:\n        raise","preventionTips":["Convert signed values with & 0xFFFFFFFF before 32-bit bit tricks.","Treat Python ints as unbounded; never assume 32-bit wrapping semantics."],"tags":["bit-manipulation","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}