Skip to content

Commit 93245ab

Browse files
Fix: handle zero input in binary_count_trailing_zeros
1 parent 840ca00 commit 93245ab

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

bit_manipulation/binary_count_trailing_zeros.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def binary_count_trailing_zeros(a: int) -> int:
1717
>>> binary_count_trailing_zeros(4294967296)
1818
32
1919
>>> binary_count_trailing_zeros(0)
20-
0
20+
Traceback (most recent call last):
21+
...
22+
ValueError: Trailing zeros undefined for zero
2123
>>> binary_count_trailing_zeros(-10)
2224
Traceback (most recent call last):
2325
...
@@ -29,16 +31,21 @@ def binary_count_trailing_zeros(a: int) -> int:
2931
>>> binary_count_trailing_zeros("0")
3032
Traceback (most recent call last):
3133
...
32-
TypeError: '<' not supported between instances of 'str' and 'int'
34+
TypeError: Input value must be a 'int' type
3335
"""
36+
if not isinstance(a, int):
37+
raise TypeError("Input value must be a 'int' type")
38+
3439
if a < 0:
3540
raise ValueError("Input value must be a positive integer")
36-
elif isinstance(a, float):
37-
raise TypeError("Input value must be a 'int' type")
38-
return 0 if (a == 0) else int(log2(a & -a))
41+
42+
if a == 0:
43+
raise ValueError("Trailing zeros undefined for zero")
44+
45+
return int(log2(a & -a))
3946

4047

4148
if __name__ == "__main__":
4249
import doctest
4350

44-
doctest.testmod()
51+
doctest.testmod()

0 commit comments

Comments
 (0)