From 111012b13ad0e4f3ed20443f89aa85abbeaf711e Mon Sep 17 00:00:00 2001 From: sDaCoder Date: Tue, 30 Sep 2025 01:25:49 +0530 Subject: [PATCH 1/3] Implemented clear_least_significant_set_bit in single_bit_manipulation_operations.py --- .../single_bit_manipulation_operations.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py index fcbf033ccb24..4b1642cc277a 100644 --- a/bit_manipulation/single_bit_manipulation_operations.py +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -93,6 +93,20 @@ def get_bit(number: int, position: int) -> int: """ return int((number & (1 << position)) != 0) +def clear_least_significant_set_bit(number: int) -> int: + """ + Clear the least significant set bit. + + Details: perform bitwise and for given number and X. + Where X is a number with all the bits - ones and bit on given + position - zero. + + >>> clear_least_significant_set_bit(0b1101) # 0b1100 + 12 + >>> clear_least_significant_set_bit(0b1111) # 0b1110 + 14 + """ + return number & (number - 1) if __name__ == "__main__": import doctest From 144b2695e8805db70133e8e2935f703f0d6c8961 Mon Sep 17 00:00:00 2001 From: sDaCoder Date: Tue, 30 Sep 2025 14:25:53 +0530 Subject: [PATCH 2/3] Added better comments to the function --- .../single_bit_manipulation_operations.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py index 4b1642cc277a..c9743bb5cc57 100644 --- a/bit_manipulation/single_bit_manipulation_operations.py +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -97,14 +97,19 @@ def clear_least_significant_set_bit(number: int) -> int: """ Clear the least significant set bit. - Details: perform bitwise and for given number and X. - Where X is a number with all the bits - ones and bit on given - position - zero. + Details: perform bitwise operation for the given number X. + Where X is in bits, and the least significant set, i.e., the rightmost 1, is cleared. >>> clear_least_significant_set_bit(0b1101) # 0b1100 12 >>> clear_least_significant_set_bit(0b1111) # 0b1110 14 + >>> clear_least_significant_set_bit(0b1100) # 0b1000 + 8 + >>> clear_least_significant_set_bit(0b0) # 0b0 + 0 + >>> clear_least_significant_set_bit(0b1) # 0b0 + 0 """ return number & (number - 1) From 7e2985b65ace7833e3e9401cb4a2d5cee6c39d3d Mon Sep 17 00:00:00 2001 From: sDaCoder Date: Tue, 30 Sep 2025 17:28:30 +0530 Subject: [PATCH 3/3] Added tests to handle negative inputs --- bit_manipulation/single_bit_manipulation_operations.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bit_manipulation/single_bit_manipulation_operations.py b/bit_manipulation/single_bit_manipulation_operations.py index c9743bb5cc57..f7cee2a310e9 100644 --- a/bit_manipulation/single_bit_manipulation_operations.py +++ b/bit_manipulation/single_bit_manipulation_operations.py @@ -93,12 +93,14 @@ def get_bit(number: int, position: int) -> int: """ return int((number & (1 << position)) != 0) + def clear_least_significant_set_bit(number: int) -> int: """ Clear the least significant set bit. Details: perform bitwise operation for the given number X. - Where X is in bits, and the least significant set, i.e., the rightmost 1, is cleared. + Where X is in bits, and the least significant set, i.e., the rightmost 1, + is cleared. >>> clear_least_significant_set_bit(0b1101) # 0b1100 12 @@ -110,9 +112,14 @@ def clear_least_significant_set_bit(number: int) -> int: 0 >>> clear_least_significant_set_bit(0b1) # 0b0 0 + >>> clear_least_significant_set_bit(-5) # -6 -> Handling the negative numbers + -6 + >>> clear_least_significant_set_bit(-6) # -8 -> Handling the negative even numbers + -8 """ return number & (number - 1) + if __name__ == "__main__": import doctest