Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

Adds the Swap Odd and Even Bits algorithm to the bit manipulation module.

This algorithm swaps the odd and even bits of a 32-bit unsigned integer using bitwise operations.

Implementation Details

  • Uses bitwise AND with masks (0xAAAAAAAA for even bits, 0x55555555 for odd bits)
  • Shifts even bits right and odd bits left to perform the swap
  • Time Complexity: O(1)
  • Space Complexity: O(1)

Testing

  • Added comprehensive unit tests covering edge cases
  • All tests pass with cargo test
  • Code formatted with cargo fmt
  • No clippy warnings

Checklist

  • I have tested the code
  • I have run cargo fmt
  • I have run cargo clippy
  • I have added documentation
  • I have added tests

@codecov-commenter
Copy link

codecov-commenter commented Dec 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.62%. Comparing base (6bed9d5) to head (ce070ff).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #964   +/-   ##
=======================================
  Coverage   95.62%   95.62%           
=======================================
  Files         338      339    +1     
  Lines       22052    22078   +26     
=======================================
+ Hits        21088    21113   +25     
- Misses        964      965    +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.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

Copy link

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

This PR adds a new Swap Odd and Even Bits algorithm to the bit manipulation module. The algorithm swaps bits at even positions (0, 2, 4, 6, etc.) with bits at odd positions (1, 3, 5, 7, etc.) in a 32-bit unsigned integer using bitwise masks and shifts. The implementation achieves O(1) time and space complexity.

Key Changes

  • Implements swap_odd_even_bits function using bitwise operations with masks 0xAAAAAAAA and 0x55555555
  • Adds comprehensive unit tests covering edge cases including all bits set, alternating patterns, and powers of two
  • Includes formatting improvements to DIRECTORY.md for consistency in naming conventions

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/bit_manipulation/swap_odd_even_bits.rs New algorithm implementation with function documentation and comprehensive unit tests
src/bit_manipulation/mod.rs Adds module declaration and public export for the new algorithm
DIRECTORY.md Adds entry for the new algorithm; includes unrelated formatting improvements for consistency (e.g., "N-Queens", "ROT13", "AES")

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

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@siriak siriak merged commit e877687 into TheAlgorithms:master Dec 10, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants