{"record":{"id":"44261ad1f72afa8a","repo":"TheAlgorithms/Python","slug":"input-value-must-be-an-int-type","errorCode":null,"errorMessage":"Input value must be an 'int' type","messagePattern":"Input value must be an 'int' type","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/highest_set_bit.py","lineNumber":21,"sourceCode":"    Returns position of the highest set bit of a number.\n    Ref - https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious\n    >>> get_highest_set_bit_position(25)\n    5\n    >>> get_highest_set_bit_position(37)\n    6\n    >>> get_highest_set_bit_position(1)\n    1\n    >>> get_highest_set_bit_position(4)\n    3\n    >>> get_highest_set_bit_position(0)\n    0\n    >>> get_highest_set_bit_position(0.8)\n    Traceback (most recent call last):\n        ...\n    TypeError: Input value must be an 'int' type\n    \"\"\"\n    if not isinstance(number, int):\n        raise TypeError(\"Input value must be an 'int' type\")\n\n    position = 0\n    while number:\n        position += 1\n        number >>= 1\n\n    return position\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":3,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/bit_manipulation/highest_set_bit.py#L3-L35","documentation":"Raised by get_highest_set_bit_position when the argument is not an int. The function finds the highest set bit by right-shifting in a loop, which requires an integer operand; floats, strings, and None are rejected up front. Note: bool passes (subclass of int) and 0 is valid, returning 0.","triggerScenarios":"Calling get_highest_set_bit_position(0.8), ('4',), or (None). Any non-int type raises; negative ints are NOT rejected and will loop forever — a separate pitfall.","commonSituations":"Passing float math results or parsed strings when computing bit widths for masks, encodings, or log2-style measurements.","solutions":["Convert at the call site: get_highest_set_bit_position(int(x)) for whole-number floats.","For width of non-negative ints, prefer number.bit_length() which is idiomatic and faster.","Also guard number >= 0 yourself — this function's type check does not protect against negatives (infinite loop)."],"exampleFix":"# before\nget_highest_set_bit_position(0.8)  # TypeError\n\n# after\nget_highest_set_bit_position(int(0.8))  # 0\n# or better for n >= 0:\nn.bit_length()","handlingStrategy":"type-guard","validationCode":"if not isinstance(number, int):\n    number = int(number)\nif number < 0:\n    raise ValueError(\"must be non-negative (function loops forever otherwise)\")","typeGuard":"def is_int(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool)","tryCatchPattern":null,"preventionTips":["Prefer number.bit_length() for the same result on non-negative ints.","Also guard against negatives — this function's own checks do not.","Static type checking (mypy/pyright) prevents float/string leaks into int params."],"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"}