{"record":{"id":"48c6453ed9718f37","repo":"TheAlgorithms/Python","slug":"the-value-of-input-must-not-be-negative","errorCode":null,"errorMessage":"the value of input must not be negative","messagePattern":"the value of input must not be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/count_number_of_one_bits.py","lineNumber":25,"sourceCode":"    >>> get_set_bits_count_using_brian_kernighans_algorithm(25)\n    3\n    >>> get_set_bits_count_using_brian_kernighans_algorithm(37)\n    3\n    >>> get_set_bits_count_using_brian_kernighans_algorithm(21)\n    3\n    >>> get_set_bits_count_using_brian_kernighans_algorithm(58)\n    4\n    >>> get_set_bits_count_using_brian_kernighans_algorithm(0)\n    0\n    >>> get_set_bits_count_using_brian_kernighans_algorithm(256)\n    1\n    >>> get_set_bits_count_using_brian_kernighans_algorithm(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: the value of input must not be negative\n    \"\"\"\n    if number < 0:\n        raise ValueError(\"the value of input must not be negative\")\n    result = 0\n    while number:\n        number &= number - 1\n        result += 1\n    return result\n\n\ndef get_set_bits_count_using_modulo_operator(number: int) -> int:\n    \"\"\"\n    Count the number of set bits in a 32 bit integer\n    >>> get_set_bits_count_using_modulo_operator(25)\n    3\n    >>> get_set_bits_count_using_modulo_operator(37)\n    3\n    >>> get_set_bits_count_using_modulo_operator(21)\n    3\n    >>> get_set_bits_count_using_modulo_operator(58)\n    4","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/count_number_of_one_bits.py#L7-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"Computing Hamming weights of signed values, error codes stored as signed ints, or mask arithmetic that underflows below zero.","solutions":["Mask to unsigned width first: get_set_bits_count_using_brian_kernighans_algorithm(number & 0xFFFFFFFF) for 32-bit values.","Validate number >= 0 at the source and fix the sign upstream.","Switch to int.bit_count() (Python 3.10+), which handles negative ints via their two's-complement concept or raises clearly."],"exampleFix":"# before\nget_set_bits_count_using_brian_kernighans_algorithm(-1)  # ValueError\n\n# after\nget_set_bits_count_using_brian_kernighans_algorithm(error_code & 0xFF)","handlingStrategy":"validation","validationCode":"if number < 0:\n    number &= 0xFFFFFFFF  # or reject, per your domain\nif not isinstance(number, int):\n    raise TypeError(\"expected int\")","typeGuard":"def is_non_negative_int(n: object) -> bool:\n    return isinstance(n, int) and n >= 0","tryCatchPattern":null,"preventionTips":["Mask signed values to a fixed width before popcount.","Guard subtraction-derived bitmasks against underflow.","Use n.bit_count() (3.10+) as the modern equivalent."],"tags":["bit-manipulation","input-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}