Skip to content

Add clear_least_significant_set_bit operation - #15305

Merged
cclauss merged 3 commits into
TheAlgorithms:masterfrom
deerred643-star:add-clear-least-significant-set-bit
Sep 13, 2026
Merged

Add clear_least_significant_set_bit operation#15305
cclauss merged 3 commits into
TheAlgorithms:masterfrom
deerred643-star:add-clear-least-significant-set-bit

Conversation

@deerred643-star

Copy link
Copy Markdown
Contributor

Describe your change

This PR adds clear_least_significant_set_bit(), which uses
number & (number - 1) to clear the rightmost set bit. The docstring explains
the bitwise operation and includes doctests for zero, powers of two, multiple
set bits, and negative integers under Python's sign-extension semantics.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Tests

  • python -m doctest -v bit_manipulation/single_bit_manipulation_operations.py
  • ruff check bit_manipulation/single_bit_manipulation_operations.py
  • ruff format --check bit_manipulation/single_bit_manipulation_operations.py

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Fixes #12970

@cclauss

cclauss commented Sep 12, 2026

Copy link
Copy Markdown
Member

ON HOLD: Our focus is on merging or closing old pull requests before October 1st.

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Clean, well-scoped addition — the n & (n - 1) trick to clear the least significant set bit is exactly right, and I like that the docstring explains why it works rather than just asserting it.

I verified it independently: ran the doctests (21/21 pass) and cross-checked n & (n-1) against n - (n & -n) (subtracting the lowest set-bit value) for positives, negatives, zero, and powers of two (0, 8, 15, 44, 255, 256, -5, -1, -256) — all match, including the two's-complement negative cases your -5 -> -6 doctest covers.

Type hints and doctests are present per the contributing guide. One tiny, non-blocking nit: the linked reference is the Kernighan bit-counting kernel; it uses the same n & (n-1) identity so it's relevant, but a reader might expect a page specifically about clearing the low bit. Fine to leave as-is.

Approving.

@cclauss
cclauss merged commit ab37831 into TheAlgorithms:master Sep 13, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a function of clear_least_significant_set_bit in bit manipulation and in file of single_bit_manipulation_operations.py

3 participants