Skip to content

Commit ecde438

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 071639b commit ecde438

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

bit_manipulation/index_of_rightmost_set_bit.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# Reference: https://www.geeksforgeeks.org/position-of-rightmost-set-bit/
22

3+
34
def get_index_of_rightmost_set_bit(number: int) -> int:
45
"""
56
Take in a positive integer 'number'.
67
Returns the zero-based index of first set bit in that 'number' from right.
78
Returns -1, If no set bit found.
89
9-
>>> get_index_of_rightmost_set_bit(0)
10+
>>> get_index_of_rightmost_set_bit(0)
1011
-1
11-
>>> get_index_of_rightmost_set_bit(5)
12+
>>> get_index_of_rightmost_set_bit(5)
1213
0
13-
>>> get_index_of_rightmost_set_bit(36)
14+
>>> get_index_of_rightmost_set_bit(36)
1415
2
15-
>>> get_index_of_rightmost_set_bit(8)
16+
>>> get_index_of_rightmost_set_bit(8)
1617
3
17-
>>> get_index_of_rightmost_set_bit(-18)
18+
>>> get_index_of_rightmost_set_bit(-18)
1819
Traceback (most recent call last):
1920
...
2021
ValueError: Input must be a non-negative integer
@@ -23,12 +24,13 @@ def get_index_of_rightmost_set_bit(number: int) -> int:
2324
if 0 > number and not isinstance(number, int):
2425
raise ValueError("Input must be a non-negative integer")
2526

26-
intermediate = number&~(number-1)
27+
intermediate = number & ~(number - 1)
2728
index = 0
2829
while intermediate:
29-
intermediate>>=1
30-
index+=1
31-
return index-1
30+
intermediate >>= 1
31+
index += 1
32+
return index - 1
33+
3234

3335
if __name__ == "__main__":
3436
"""

0 commit comments

Comments
 (0)