TheAlgorithms/Python · error · TypeError
Input value must be an 'int' type
Error message
Input value must be an 'int' type
What it means
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.
Source
Thrown at bit_manipulation/highest_set_bit.py:21
Returns position of the highest set bit of a number.
Ref - https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
>>> get_highest_set_bit_position(25)
5
>>> get_highest_set_bit_position(37)
6
>>> get_highest_set_bit_position(1)
1
>>> get_highest_set_bit_position(4)
3
>>> get_highest_set_bit_position(0)
0
>>> get_highest_set_bit_position(0.8)
Traceback (most recent call last):
...
TypeError: Input value must be an 'int' type
"""
if not isinstance(number, int):
raise TypeError("Input value must be an 'int' type")
position = 0
while number:
position += 1
number >>= 1
return position
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
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).
Example fix
# before get_highest_set_bit_position(0.8) # TypeError # after get_highest_set_bit_position(int(0.8)) # 0 # or better for n >= 0: n.bit_length()
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(number, int):
number = int(number)
if number < 0:
raise ValueError("must be non-negative (function loops forever otherwise)") Type guard
def is_int(value: object) -> bool:
return isinstance(value, int) and not isinstance(value, bool) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Passing float math results or parsed strings when computing bit widths for masks, encodings, or log2-style measurements.
Related errors
- Input value must be a 'int' type
- Both arguments MUST be integers!
- Input must be a non-negative integer
- Input must be a non-negative integer
- all elements must be integers
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/44261ad1f72afa8a.
Report an issue: GitHub.