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 rightmost_set_bit function#975

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 18, 2025
edited
Loading

Description

Addsrightmost_set_bit function that finds the 1-based position of the rightmost (least significant) set bit in a positive integer.

Algorithm

Usesn & -n to isolate the rightmost set bit, thentrailing_zeros() + 1 for position.

Example: Forn = 18 (binary:10010):

  • n & -n = 2 (binary:10)
  • trailing_zeros(2) = 1
  • position = 1 + 1 = 2

Features

  • $O(1)$ time/space complexity
  • ✅ Hardware-optimized (uses BSF/CTZ instruction)
  • ✅ Two implementations (trailing_zeros and log2)
  • ✅ Comprehensive error handling
  • ✅ 15+ test cases

Examples

rightmost_set_bit(18)// Ok(2)  - binary: 10010rightmost_set_bit(12)// Ok(3)  - binary: 1100rightmost_set_bit(5)// Ok(1)  - binary: 101rightmost_set_bit(0)// Err    - no set bits

Testing

All tests pass including:

  • Powers of two
  • Odd numbers (always position 1)
  • Even numbers
  • Edge cases (zero, negatives)
  • Large numbers
  • Both implementations match

Checklist

  • Follows Rust style guidelines
  • Passes clippy lints
  • All tests pass
  • Documentation with examples
  • Properly exported

@AliAlimohammadi
Copy link
ContributorAuthor

@siriak, this is ready to be merged.

@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is98.66667% with1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.72%. Comparing base (7a261d7) to head (4798d6c).

Files with missing linesPatch %Lines
src/bit_manipulation/rightmost_set_bit.rs98.66%1 Missing⚠️
Additional details and impacted files
@@           Coverage Diff           @@##           master     #975   +/-   ##=======================================  Coverage   95.71%   95.72%           =======================================  Files         346      347    +1       Lines       22608    22683   +75     =======================================+ Hits        21639    21713   +74- Misses        969      970    +1

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

@siriaksiriak merged commit1b3d80d intoTheAlgorithms:masterDec 18, 2025
7 checks passed
@AliAlimohammadiAliAlimohammadi deleted the add-rightmost-set-bit branchDecember 18, 2025 21:17
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