Skip to content

Fix: handle zero input in binary_count_trailing_zeros#14510

Open
kadambari25 wants to merge 3 commits intoTheAlgorithms:masterfrom
kadambari25:fix-trailing-zero-bug
Open

Fix: handle zero input in binary_count_trailing_zeros#14510
kadambari25 wants to merge 3 commits intoTheAlgorithms:masterfrom
kadambari25:fix-trailing-zero-bug

Conversation

@kadambari25
Copy link
Copy Markdown

Description

This PR fixes an issue in the function binary_count_trailing_zeros.

Previously, the function returned 0 for input value 0. However, mathematically, trailing zeros for 0 are undefined, and returning 0 can lead to incorrect behavior in dependent algorithms.

Changes made

  • Added validation for input 0
  • Raised ValueError when input is zero
  • Improved type checking
  • Updated doctest examples accordingly

Fixes #14479


Type of change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • 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.
  • All filenames are in lowercase with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with type hints.
  • All functions have doctests that pass automated testing.
  • This PR includes the related issue number (Fixes fix: off-by-one error in binary_count_trailing_zeros for zero input #14479).

@algorithms-keeper algorithms-keeper bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Apr 6, 2026
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 6, 2026
@kadambari25 kadambari25 force-pushed the fix-trailing-zero-bug branch from bcf8625 to dd91001 Compare April 6, 2026 10:49
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