Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add binary count trailing zeros algorithm#972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@AliAlimohammadi
Copy link
Contributor

@AliAlimohammadiAliAlimohammadi commentedDec 17, 2025
edited
Loading

Description

Adds an algorithm to count trailing zeros in the binary representation of a number.

Algorithm

Counts the number of consecutive zeros from the least significant bit (rightmost) in the binary representation of an unsigned integer.

Time Complexity: O(1) - uses CPU instruction
Space Complexity: O(1)

Example

assert_eq!(binary_count_trailing_zeros(25),0);// 11001 -> 0 trailing zerosassert_eq!(binary_count_trailing_zeros(36),2);// 100100 -> 2 trailing zerosassert_eq!(binary_count_trailing_zeros(16),4);// 10000 -> 4 trailing zerosassert_eq!(binary_count_trailing_zeros(58),1);// 111010 -> 1 trailing zero

Implementation Details

  • Primary: Uses Rust's built-intrailing_zeros() method
  • Alternative: Bitwise implementation usinglog2(num & -num) trick
  • Compiles to single CPU instruction (BSF/TZCNT on x86)
  • Handles zero case explicitly to return 0
  • Type-safe: usesu64 for compile-time guarantees

Testing

  • All existing tests pass
  • Added test suite covering:
    • Basic cases from Python examples
    • Zero handling
    • Powers of 2
    • Comprehensive bitwise vs built-in comparison (22 test cases)

Checklist

  • Code follows the repository's style guidelines
  • Self-review performed
  • Code is well-commented
  • Tests added and passing
  • Documentation added

@codecov-commenter
Copy link

codecov-commenter commentedDec 17, 2025
edited
Loading

Codecov Report

❌ Patch coverage is97.43590% with1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.70%. Comparing base (5c4593c) to head (28d769d).

Files with missing linesPatch %Lines
...rc/bit_manipulation/binary_count_trailing_zeros.rs97.43%1 Missing⚠️
Additional details and impacted files
@@           Coverage Diff           @@##           master     #972   +/-   ##=======================================  Coverage   95.70%   95.70%           =======================================  Files         345      346    +1       Lines       22569    22608   +39     =======================================+ Hits        21599    21638   +39  Misses        970      970

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
ContributorAuthor

@siriak, this is ready to be merged.

@siriaksiriak merged commit7a261d7 intoTheAlgorithms:masterDec 17, 2025
7 checks passed
@AliAlimohammadiAliAlimohammadi deleted the add-binary-trailing-zeros branchDecember 17, 2025 23:11
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@siriaksiriaksiriak approved these changes

@imp2002imp2002Awaiting requested review from imp2002imp2002 is a code owner

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@AliAlimohammadi@codecov-commenter@siriak

[8]ページ先頭

©2009-2025 Movatter.jp