{"record":{"id":"9aec39dcdf42c9c0","repo":"TheAlgorithms/Python","slug":"number-must-be-positive","errorCode":null,"errorMessage":"number must be positive","messagePattern":"number must be positive","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/power_of_4.py","lineNumber":53,"sourceCode":"    False\n    >>> power_of_4(17)\n    False\n    >>> power_of_4(64)\n    True\n    >>> power_of_4(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: number must be positive\n    >>> power_of_4(1.2)\n    Traceback (most recent call last):\n        ...\n    TypeError: number must be an integer\n\n    \"\"\"\n    if not isinstance(number, int):\n        raise TypeError(\"number must be an integer\")\n    if number <= 0:\n        raise ValueError(\"number must be positive\")\n    if number & (number - 1) == 0:\n        c = 0\n        while number:\n            c += 1\n            number >>= 1\n        return c % 2 == 1\n    else:\n        return False\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":35,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/power_of_4.py#L35-L68","documentation":"Raised by power_of_4() when the argument passes the isinstance(number, int) check but is zero or negative. The function only defines 'power of 4' for positive integers (1, 4, 16, 64, ...), so any non-positive value is rejected before the bit-trick logic runs.","triggerScenarios":"Calling power_of_4(0), power_of_4(-4), or any int <= 0. Note booleans pass too since bool is a subclass of int, but power_of_4(False) == power_of_4(0) also triggers it.","commonSituations":"Passing a computed index/offset that can be 0, forwarding user input without range validation, or porting code from a language where 0 is acceptable.","solutions":["Pass a positive integer: power_of_4(4) -> True.","If 0/negative can occur, clamp or reject upstream: max(1, n) or an explicit if n <= 0 guard.","For float support questions, note floats raise TypeError ('number must be an integer') instead — convert with int() first if that is intended."],"exampleFix":"# before\npower_of_4(0)  # ValueError: number must be positive\n\n# after\nif n > 0:\n    power_of_4(n)\nelse:\n    raise ValueError(f\"expected positive int, got {n}\")","handlingStrategy":"validation","validationCode":"def is_valid_power_of_4_arg(n) -> bool:\n    return isinstance(n, int) and n > 0","typeGuard":"def is_positive_int(n: object) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n > 0","tryCatchPattern":"try:\n    power_of_4(n)\nexcept ValueError as e:\n    if 'positive' in str(e):\n        handle_nonpositive(n)\n    else:\n        raise","preventionTips":["Validate n > 0 at the data source before calling bit-manipulation helpers.","Remember floats raise TypeError here — convert deliberately with int()."],"tags":["bit-manipulation","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}