{"record":{"id":"58ed9c96985df68c","repo":"TheAlgorithms/Python","slug":"number-must-be-an-integer","errorCode":null,"errorMessage":"number must be an integer","messagePattern":"number must be an integer","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/power_of_4.py","lineNumber":51,"sourceCode":"    False\n    >>> power_of_4(8)\n    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":33,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/power_of_4.py#L33-L68","documentation":"Raised by power_of_4 when the argument is not an int. The function first checks the type (this TypeError), then positivity (separate ValueError for number <= 0), then uses bit tricks — number & (number - 1) == 0 plus an odd bit-length — that only make sense for positive integers. Note bool passes the isinstance check.","triggerScenarios":"Calling power_of_4(1.2), ('16',), or (None). power_of_4(-1) raises the sibling ValueError instead; power_of_4(0) also raises ValueError.","commonSituations":"Validating numeric config parsed from strings/JSON; passing float math results when checking alignment or power-of-four constraints (image dims, texture sizes, bucket counts).","solutions":["Convert at the call site: power_of_4(int(value)) for whole-number inputs.","Validate dimension/size inputs as positive ints where they enter the program.","Inline alternative without the helper: n > 0 and n & (n - 1) == 0 and n.bit_length() % 2 == 1."],"exampleFix":"# before\npower_of_4(1.2)  # TypeError\n\n# after\npower_of_4(int(1.2)) if float(1.2).is_integer() else False","handlingStrategy":"type-guard","validationCode":"if not isinstance(number, int) or isinstance(number, bool):\n    raise TypeError(\"number must be an integer\")\nif number <= 0:\n    raise ValueError(\"number must be positive\")","typeGuard":"def is_positive_int(n: object) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n > 0","tryCatchPattern":null,"preventionTips":["int() parsed sizes/dimensions before power-of-four checks.","Validate texture/bucket dimensions at the boundary as positive ints.","Inline the bit test `n > 0 and n & (n-1) == 0 and n.bit_length() % 2 == 1` to avoid wrapper edge cases."],"tags":["bit-manipulation","type-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}