Skip to content

Commit 7e2985b

Browse files
committed
Added tests to handle negative inputs
1 parent 144b269 commit 7e2985b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

bit_manipulation/single_bit_manipulation_operations.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ def get_bit(number: int, position: int) -> int:
9393
"""
9494
return int((number & (1 << position)) != 0)
9595

96+
9697
def clear_least_significant_set_bit(number: int) -> int:
9798
"""
9899
Clear the least significant set bit.
99100
100101
Details: perform bitwise operation for the given number X.
101-
Where X is in bits, and the least significant set, i.e., the rightmost 1, is cleared.
102+
Where X is in bits, and the least significant set, i.e., the rightmost 1,
103+
is cleared.
102104
103105
>>> clear_least_significant_set_bit(0b1101) # 0b1100
104106
12
@@ -110,9 +112,14 @@ def clear_least_significant_set_bit(number: int) -> int:
110112
0
111113
>>> clear_least_significant_set_bit(0b1) # 0b0
112114
0
115+
>>> clear_least_significant_set_bit(-5) # -6 -> Handling the negative numbers
116+
-6
117+
>>> clear_least_significant_set_bit(-6) # -8 -> Handling the negative even numbers
118+
-8
113119
"""
114120
return number & (number - 1)
115121

122+
116123
if __name__ == "__main__":
117124
import doctest
118125

0 commit comments

Comments
 (0)