{"record":{"id":"eb3b3a1bf49a69fa","repo":"TheAlgorithms/Python","slug":"number-must-not-be-negative","errorCode":null,"errorMessage":"number must not be negative","messagePattern":"number must not be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/is_power_of_two.py","lineNumber":50,"sourceCode":"    >>> is_power_of_two(8)\n    True\n    >>> is_power_of_two(17)\n    False\n    >>> is_power_of_two(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: number must not be negative\n    >>> is_power_of_two(1.2)\n    Traceback (most recent call last):\n        ...\n    TypeError: unsupported operand type(s) for &: 'float' and 'float'\n\n    # Test all powers of 2 from 0 to 10,000\n    >>> all(is_power_of_two(int(2 ** i)) for i in range(10000))\n    True\n    \"\"\"\n    if number < 0:\n        raise ValueError(\"number must not be negative\")\n    return number & (number - 1) == 0\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":32,"sourceCodeEnd":58,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/is_power_of_two.py#L32-L58","documentation":"Raised by is_power_of_two when number is negative. The classic check number & (number - 1) == 0 only identifies powers of two for non-negative integers, so negatives are rejected. There is no type guard: floats like 1.2 pass this check and fail later with a TypeError from the & operator.","triggerScenarios":"Calling is_power_of_two(-1), (-16), or any negative int. is_power_of_two(1.2) does NOT raise this — it raises TypeError from `&` between floats. Also note is_power_of_two(0) returns True (quirk of the same guard).","commonSituations":"Validating sizes, capacities, or alignment values that can be configured negative; passing signed deltas into a power-of-two check.","solutions":["Validate at the boundary: require value > 0 (and int) before calling if a true power of two is expected.","Handle the 0 case explicitly if is_power_of_two(0) == True would be wrong for your logic.","Use n > 0 and (n & (n - 1)) == 0 inline for a guard-free, well-defined check on any int."],"exampleFix":"# before\nis_power_of_two(size)  # ValueError when size < 0\n\n# after\nis_power_of_two(size) if size >= 0 else False","handlingStrategy":"validation","validationCode":"if not isinstance(number, int) or number <= 0:\n    is_pow2 = False\nelse:\n    is_pow2 = is_power_of_two(number)","typeGuard":"def is_positive_int(n: object) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n > 0","tryCatchPattern":null,"preventionTips":["Check n > 0 yourself — the library accepts 0 and returns True.","Keep sizes/capacities as validated positive ints from config load onward.","Inline `n > 0 and n & (n - 1) == 0` removes all guard edge cases."],"tags":["bit-manipulation","input-validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}