TheAlgorithms/Python · error · TypeError

Input value must be an 'int' type

Error message

Input value must be an 'int' type

What it means

Raised by reverse_bit() when the argument is not an int. The function reverses the 32 bits of an integer with shifts (<<=, >>=, & 1), which only make sense for ints, so floats, strings, and bools-as-intent are rejected upfront. Note bool technically passes since isinstance(True, int) is True.

Source

Thrown at bit_manipulation/reverse_bits.py:65

    >>> reverse_bit(2550136832)
    25
    >>> 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__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert first: reverse_bit(int(value)) when the value is a numeric string or whole float.
  2. Add isinstance validation in calling code before invoking reverse_bit.
  3. Keep the input in the documented domain: a non-negative 32-bit int.

Example fix

# before
reverse_bit("0")  # TypeError: Input value must be an 'int' type

# after
reverse_bit(int("0"))  # 0
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(number, int):
    raise TypeError(f'reverse_bit expects int, got {type(number).__name__}')

Type guard

def is_reversable_int(n: object) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n >= 0

Try / catch

try:
    reverse_bit(n)
except TypeError:
    n = int(n)
    result = reverse_bit(n)

Prevention

When it happens

Trigger: Calling reverse_bit(1.1) or reverse_bit("0") as shown in the doctests; also reverse_bit(None) or reverse_bit([1]).

Common situations: Parsing numeric options from argv or JSON that arrive as strings, or mixing APIs that return floats with this int-only bit utility.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/74146477f7ffd5e7. Report an issue: GitHub.