-
Notifications
You must be signed in to change notification settings - Fork 20.5k
refactor: Enhance docs, add more tests in SwapAdjacentBits
#5861
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
44 changes: 34 additions & 10 deletions
44
src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
package com.thealgorithms.bitmanipulation; | ||
|
||
/** | ||
* Swap every pair of adjacent bits of a given number. | ||
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999) | ||
* A utility class to swap every pair of adjacent bits in a given integer. | ||
* This operation shifts the even-positioned bits to odd positions and vice versa. | ||
* | ||
* Example: | ||
* - Input: 2 (binary: `10`) → Output: 1 (binary: `01`) | ||
* - Input: 43 (binary: `101011`) → Output: 23 (binary: `010111`) | ||
* | ||
* **Explanation of the Algorithm:** | ||
* 1. Mask even-positioned bits: Using `0xAAAAAAAA` (binary: `101010...`), | ||
* which selects bits in even positions. | ||
* 2. Mask odd-positioned bits: Using `0x55555555` (binary: `010101...`), | ||
* which selects bits in odd positions. | ||
* 3. Shift bits: | ||
* - Right-shift even-positioned bits by 1 to move them to odd positions. | ||
* - Left-shift odd-positioned bits by 1 to move them to even positions. | ||
* 4. Combine both shifted results using bitwise OR (`|`) to produce the final result. | ||
* | ||
* Use Case: This algorithm can be useful in applications involving low-level bit manipulation, | ||
* such as encoding, data compression, or cryptographic transformations. | ||
* | ||
* Time Complexity: O(1) (constant time, since operations are bitwise). | ||
* | ||
* Author: Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999) | ||
*/ | ||
|
||
public final class SwapAdjacentBits { | ||
private SwapAdjacentBits() { | ||
} | ||
|
||
/** | ||
* Swaps every pair of adjacent bits of a given integer. | ||
* Steps: | ||
* 1. Mask the even-positioned bits. | ||
* 2. Mask the odd-positioned bits. | ||
* 3. Shift the even bits to the right and the odd bits to the left. | ||
* 4. Combine the shifted bits. | ||
* | ||
* @param num the integer whose bits are to be swapped | ||
* @return the integer after swapping every pair of adjacent bits | ||
*/ | ||
public static int swapAdjacentBits(int num) { | ||
// mask the even bits (0xAAAAAAAA => 10101010...) | ||
int evenBits = num & 0xAAAAAAAA; | ||
|
||
// mask the odd bits (0x55555555 => 01010101...) | ||
int oddBits = num & 0x55555555; | ||
|
||
// right shift even bits and left shift odd bits | ||
evenBits >>= 1; | ||
oddBits <<= 1; | ||
|
||
// combine shifted bits | ||
return evenBits | oddBits; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.