TheAlgorithms/Python · error · ValueError
The value of input must be non-negative
Error message
The value of input must be non-negative
What it means
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.
Source
Thrown at bit_manipulation/reverse_bits.py:67
>>> reverse_bit(-1)
Traceback (most recent call last):
...
ValueError: The value of input must be non-negative
>>> reverse_bit(1.1)
Traceback (most recent call last):
...
TypeError: Input value must be an 'int' type
>>> reverse_bit("0")
Traceback (most recent call last):
...
TypeError: Input value must be an 'int' type
"""
if not isinstance(number, int):
raise TypeError("Input value must be an 'int' type")
if number < 0:
raise ValueError("The value of input must be non-negative")
result = 0
# iterator over [0 to 31], since we are dealing with a 32 bit integer
for _ in range(32):
# left shift the bits by unity
result <<= 1
# get the end bit
end_bit = number & 1
# right shift the bits by unity
number >>= 1
# add that bit to our answer
result |= end_bit
return result
if __name__ == "__main__":
import doctest
View on GitHub (pinned to f5988cc097)
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.
Example fix
# before reverse_bit(-3) # ValueError: The value of input must be non-negative # after reverse_bit(-3 & 0xFFFFFFFF) # reverses the 32-bit two's-complement pattern
Defensive patterns
Strategy: validation
Validate before calling
if number < 0:
number &= 0xFFFFFFFF # reinterpret as unsigned 32-bit
assert 0 <= number < 2**32 Type guard
def is_uint32(n: object) -> bool:
return isinstance(n, int) and not isinstance(n, bool) and 0 <= n < 2**32 Try / catch
try:
reverse_bit(n)
except ValueError as e:
if 'non-negative' in str(e):
n &= 0xFFFFFFFF
result = reverse_bit(n)
else:
raise Prevention
- Convert signed values with & 0xFFFFFFFF before 32-bit bit tricks.
- Treat Python ints as unbounded; never assume 32-bit wrapping semantics.
When it happens
Trigger: Calling reverse_bit(-1) or any negative int. Note this check runs after the isinstance check, so reverse_bit(-1.0) raises TypeError first.
Common situations: 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.
Related errors
- number must be positive
- Input value must be a 'int' type
- the value of both inputs must be positive
- both inputs must be positive integers
- input must be a negative integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/17fecc5b1fa2ed26.
Report an issue: GitHub.