feat: add binary shift operations #988
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds implementations of four types of binary shift operations to the
bit_manipulationmodule, with binary string output for visualization and educational purposes.Implementation Details
The implementation provides four main functions:
logical_left_shift(number: i32, shift_amount: i32) -> Result<String, String>logical_right_shift(number: i32, shift_amount: i32) -> Result<String, String>arithmetic_left_shift(number: i32, shift_amount: i32) -> Result<String, String>arithmetic_right_shift(number: i32, shift_amount: i32) -> Result<String, String>Algorithm Details
Logical Left Shift:
Logical Right Shift:
Arithmetic Left Shift:
Arithmetic Right Shift:
Key Features
Resultfor error handlingShift Types Explained
Logical Left Shift (<<)
Arithmetic Left Shift (<<)
Logical Right Shift (>>> in some languages)
Arithmetic Right Shift (>>)
Two's Complement Implementation
For negative numbers in arithmetic right shift, the implementation follows Python's approach:
abs(number) - (1 << binary_length)This ensures correct representation of negative numbers in binary form.
Changes Made
src/bit_manipulation/binary_shifts.rswith complete implementationsrc/bit_manipulation/mod.rsto include and export the moduleType of Change
Test Coverage
✅ 35+ Unit Tests Including:
Checklist
cargo fmtcargo clippymod.rsAdditional Context
Resulttype for error handling instead of exceptionsReferences