Skip to content

Commit 6393340

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

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

bit_manipulation/index_of_rightmost_set_bit.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
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
"""
67
Take in a positive integer 'number'.
78
Returns the zero-based index of first set bit in that 'number' from right.
89
Returns -1, If no set bit found.
910
10-
>>> get_index_of_rightmost_set_bit(0)
11+
>>> get_index_of_rightmost_set_bit(0)
1112
-1
12-
>>> get_index_of_rightmost_set_bit(5)
13+
>>> get_index_of_rightmost_set_bit(5)
1314
0
14-
>>> get_index_of_rightmost_set_bit(36)
15+
>>> get_index_of_rightmost_set_bit(36)
1516
2
16-
>>> get_index_of_rightmost_set_bit(8)
17+
>>> get_index_of_rightmost_set_bit(8)
1718
3
18-
>>> get_index_of_rightmost_set_bit(-18)
19+
>>> get_index_of_rightmost_set_bit(-18)
1920
Traceback (most recent call last):
2021
...
2122
ValueError: Input must be a non-negative integer
2223
"""
2324

24-
if number>=0 and type(number)==int:
25-
intermediate = number&~(number-1)
25+
if number >= 0 and type(number) == int:
26+
intermediate = number & ~(number - 1)
2627
index = 0
2728
while intermediate:
28-
intermediate>>=1
29-
index+=1
30-
return index-1
31-
else: raise ValueError("Input must be a non-negative integer")
29+
intermediate >>= 1
30+
index += 1
31+
return index - 1
32+
else:
33+
raise ValueError("Input must be a non-negative integer")
34+
3235

3336
"""
3437
Finding the index of rightmost set bit has some very peculiar use-cases,

0 commit comments

Comments
 (0)