TheAlgorithms-Python
34 строки · 850.0 Байт
1def get_highest_set_bit_position(number: int) -> int:2"""3Returns position of the highest set bit of a number.
4Ref - https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
5>>> get_highest_set_bit_position(25)
65
7>>> get_highest_set_bit_position(37)
86
9>>> get_highest_set_bit_position(1)
101
11>>> get_highest_set_bit_position(4)
123
13>>> get_highest_set_bit_position(0)
140
15>>> get_highest_set_bit_position(0.8)
16Traceback (most recent call last):
17...
18TypeError: Input value must be an 'int' type
19"""
20if not isinstance(number, int):21raise TypeError("Input value must be an 'int' type")22
23position = 024while number:25position += 126number >>= 127
28return position29
30
31if __name__ == "__main__":32import doctest33
34doctest.testmod()35