{"record":{"id":"ea7554b94ea72547","repo":"TheAlgorithms/Python","slug":"input-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"Input must be a non-negative integer","messagePattern":"Input must be a non-negative integer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/count_1s_brian_kernighan_method.py","lineNumber":31,"sourceCode":"    >>> get_1s_count(0)\n    0\n    >>> get_1s_count(256)\n    1\n    >>> get_1s_count(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a non-negative integer\n    >>> get_1s_count(0.8)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a non-negative integer\n    >>> get_1s_count(\"25\")\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a non-negative integer\n    \"\"\"\n    if not isinstance(number, int) or number < 0:\n        raise ValueError(\"Input must be a non-negative integer\")\n\n    count = 0\n    while number:\n        # This way we arrive at next set bit (next 1) instead of looping\n        # through each bit and checking for 1s hence the\n        # loop won't run 32 times it will only run the number of `1` times\n        number &= number - 1\n        count += 1\n    return count\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":13,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/count_1s_brian_kernighan_method.py#L13-L47","documentation":"Raised by get_1s_count (Brian Kernighan popcount) when the input is not an int or is negative. The loop `number &= number - 1` relies on integer semantics and terminates only for non-negative values, so both wrong types and negative values are rejected with a single ValueError.","triggerScenarios":"Calling get_1s_count(0.8), get_1s_count('25'), or get_1s_count(-3). Any non-int type or negative integer triggers it.","commonSituations":"Counting set bits in data that arrived as strings or floats (CSV parsing, JSON numbers, len() results used as bitmasks); assuming the function does its own coercion.","solutions":["Convert first: get_1s_count(int(value)) when value is a numeric string or whole float.","For bitmasks built from ranges, ensure you pass range bounds or indices that are already ints.","Prefer Python 3.10+ int.bit_count() which is faster and equally strict."],"exampleFix":"# before\nget_1s_count('25')  # ValueError\n\n# after\nget_1s_count(int('25'))  # 3","handlingStrategy":"validation","validationCode":"if not isinstance(number, int) or isinstance(number, bool) or number < 0:\n    raise ValueError(\"popcount needs a non-negative int\")","typeGuard":"def is_non_negative_int(n: object) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 0","tryCatchPattern":null,"preventionTips":["int() numeric strings and floats before counting bits.","Prefer int.bit_count() on Python 3.10+.","Validate parsed data types at the system boundary, not inside algorithms."],"tags":["bit-manipulation","input-validation","type-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}