Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bit_manipulation/single_bit_manipulation_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ 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 (rightmost 1 bit).

Subtracting 1 changes the rightmost 1 to 0 and the 0 bits to its right to 1.
ANDing the result with the original number therefore clears that set bit.
For negative integers, Python's infinite sign extension is used.
https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan

>>> clear_least_significant_set_bit(0b101100) # 0b101000
40
>>> clear_least_significant_set_bit(0b1000) # 0b0
0
>>> clear_least_significant_set_bit(0)
0
>>> clear_least_significant_set_bit(0b1111) # 0b1110
14
>>> clear_least_significant_set_bit(-5)
-6
"""
return number & (number - 1)


if __name__ == "__main__":
import doctest

Expand Down
Loading