TheAlgorithms/Python · error · ValueError

the value of input must not be negative

Error message

the value of input must not be negative

What it means

Raised by get_set_bits_count_using_brian_kernighans_algorithm when number is negative. The Kernighan loop clears the lowest set bit each iteration and only terminates for non-negative integers; negative inputs would loop incorrectly, so they are rejected. Type errors are not guarded here — non-int inputs fail at the `number < 0` comparison with a different TypeError.

Source

Thrown at bit_manipulation/count_number_of_one_bits.py:25

    >>> get_set_bits_count_using_brian_kernighans_algorithm(25)
    3
    >>> get_set_bits_count_using_brian_kernighans_algorithm(37)
    3
    >>> get_set_bits_count_using_brian_kernighans_algorithm(21)
    3
    >>> get_set_bits_count_using_brian_kernighans_algorithm(58)
    4
    >>> get_set_bits_count_using_brian_kernighans_algorithm(0)
    0
    >>> get_set_bits_count_using_brian_kernighans_algorithm(256)
    1
    >>> get_set_bits_count_using_brian_kernighans_algorithm(-1)
    Traceback (most recent call last):
        ...
    ValueError: the value of input must not be negative
    """
    if number < 0:
        raise ValueError("the value of input must not be negative")
    result = 0
    while number:
        number &= number - 1
        result += 1
    return result


def get_set_bits_count_using_modulo_operator(number: int) -> int:
    """
    Count the number of set bits in a 32 bit integer
    >>> get_set_bits_count_using_modulo_operator(25)
    3
    >>> get_set_bits_count_using_modulo_operator(37)
    3
    >>> get_set_bits_count_using_modulo_operator(21)
    3
    >>> get_set_bits_count_using_modulo_operator(58)
    4

View on GitHub (pinned to f5988cc097)

Solutions

  1. Mask to unsigned width first: get_set_bits_count_using_brian_kernighans_algorithm(number & 0xFFFFFFFF) for 32-bit values.
  2. Validate number >= 0 at the source and fix the sign upstream.
  3. Switch to int.bit_count() (Python 3.10+), which handles negative ints via their two's-complement concept or raises clearly.

Example fix

# before
get_set_bits_count_using_brian_kernighans_algorithm(-1)  # ValueError

# after
get_set_bits_count_using_brian_kernighans_algorithm(error_code & 0xFF)
Defensive patterns

Strategy: validation

Validate before calling

if number < 0:
    number &= 0xFFFFFFFF  # or reject, per your domain
if not isinstance(number, int):
    raise TypeError("expected int")

Type guard

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

Prevention

When it happens

Trigger: Calling the function with any negative int, e.g. get_set_bits_count_using_brian_kernighans_algorithm(-1). Strings/floats fail earlier at `number < 0` with a comparison TypeError.

Common situations: Computing Hamming weights of signed values, error codes stored as signed ints, or mask arithmetic that underflows below zero.

Related errors


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