{"record":{"id":"cf7af3e5e59256b8","repo":"TheAlgorithms/Python","slug":"input-must-be-a-non-negative-integer-cf7af3","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/find_previous_power_of_two.py","lineNumber":18,"sourceCode":"def find_previous_power_of_two(number: int) -> int:\n    \"\"\"\n    Find the largest power of two that is less than or equal to a given integer.\n    https://stackoverflow.com/questions/1322510\n\n    >>> [find_previous_power_of_two(i) for i in range(18)]\n    [0, 1, 2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 16, 16]\n    >>> find_previous_power_of_two(-5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input must be a non-negative integer\n    >>> find_previous_power_of_two(10.5)\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    if number == 0:\n        return 0\n    power = 1\n    while power <= number:\n        power <<= 1  # Equivalent to multiplying by 2\n    return power >> 1 if number > 1 else 1\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/find_previous_power_of_two.py#L1-L31","documentation":"Raised by find_previous_power_of_two when the input is not an int or is negative. The function doubles a power counter until it exceeds the number, logic that requires a non-negative integer; both bad types and negative values raise the same ValueError.","triggerScenarios":"Calling find_previous_power_of_two(-5) or find_previous_power_of_two(10.5). 0 is valid and returns 0.","commonSituations":"Sizing buffers/caches from computed capacities (e.g., ratio or average) that come out as floats; passing user-configured sizes without validation.","solutions":["Round down whole floats first: find_previous_power_of_two(int(size)) or math.floor for floats.","Validate configuration values (size >= 0, integer) at load time before use.","For bit_length-capable inputs, use 1 << (n.bit_length() - 1) as a direct equivalent for n >= 2."],"exampleFix":"# before\nfind_previous_power_of_two(10.5)  # ValueError\n\n# after\nfind_previous_power_of_two(int(10.5))  # 8","handlingStrategy":"validation","validationCode":"import math\nsize = int(math.floor(size))\nif size < 0:\n    raise ValueError(\"size must be non-negative\")","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":["Floor computed capacities with int() / math.floor before power-of-two rounding.","Validate config-supplied sizes at load time.","Know the closed form 1 << (n.bit_length() - 1) for n >= 2."],"tags":["bit-manipulation","input-validation","type-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}