Skip to content

fix: off-by-one error in binary_count_trailing_zeros for zero input (#14479)#14480

Open
zendy199x wants to merge 1 commit intoTheAlgorithms:masterfrom
zendy199x:fix/off-by-one-error-in-binary-count-trailing-zeros-fo
Open

fix: off-by-one error in binary_count_trailing_zeros for zero input (#14479)#14480
zendy199x wants to merge 1 commit intoTheAlgorithms:masterfrom
zendy199x:fix/off-by-one-error-in-binary-count-trailing-zeros-fo

Conversation

@zendy199x
Copy link
Copy Markdown

Fixes #14479

Describe your change:

  • 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?

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".

Copilot AI review requested due to automatic review settings March 29, 2026 06:26
@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Mar 29, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates binary_count_trailing_zeros to treat 0 as invalid input (instead of returning 0), aligning behavior with the mathematical undefined/infinite nature of trailing zeros for zero.

Changes:

  • Updated doctest to expect a ValueError for binary_count_trailing_zeros(0).
  • Removed the a == 0 fallback return path so the function no longer returns 0 for zero input.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Mar 29, 2026
"The function returns `0` for `a == 0`, but mathematically, trailing zeros in binary representation of 0 are undefined (or conventionally treated as infinite). While `log2(a & -a)` fails for `a=0`, the current fallback to `0` is inconsistent with the documented behavior: `binary_count_trailing_zeros(16)` returns `4`, but `0` has infinitely many trailing zeros — returning `0` is misleading and could cause bugs in algorithms relying on this (e.g., bit manipulation loops expecting correct trailing zero counts)."

Signed-off-by: Zendy <50132805+zendy199x@users.noreply.github.com>
@zendy199x zendy199x force-pushed the fix/off-by-one-error-in-binary-count-trailing-zeros-fo branch from 37b7435 to fcf063b Compare March 29, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: off-by-one error in binary_count_trailing_zeros for zero input

2 participants