Skip to content

Commit 54567e2

Browse files
authored
Enhance docs, add more tests in SwapAdjacentBits (#5861)
1 parent b31bc86 commit 54567e2

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* Swap every pair of adjacent bits of a given number.
5-
* @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
4+
* A utility class to swap every pair of adjacent bits in a given integer.
5+
* This operation shifts the even-positioned bits to odd positions and vice versa.
6+
*
7+
* Example:
8+
* - Input: 2 (binary: `10`) → Output: 1 (binary: `01`)
9+
* - Input: 43 (binary: `101011`) → Output: 23 (binary: `010111`)
10+
*
11+
* **Explanation of the Algorithm:**
12+
* 1. Mask even-positioned bits: Using `0xAAAAAAAA` (binary: `101010...`),
13+
* which selects bits in even positions.
14+
* 2. Mask odd-positioned bits: Using `0x55555555` (binary: `010101...`),
15+
* which selects bits in odd positions.
16+
* 3. Shift bits:
17+
* - Right-shift even-positioned bits by 1 to move them to odd positions.
18+
* - Left-shift odd-positioned bits by 1 to move them to even positions.
19+
* 4. Combine both shifted results using bitwise OR (`|`) to produce the final result.
20+
*
21+
* Use Case: This algorithm can be useful in applications involving low-level bit manipulation,
22+
* such as encoding, data compression, or cryptographic transformations.
23+
*
24+
* Time Complexity: O(1) (constant time, since operations are bitwise).
25+
*
26+
* Author: Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999)
627
*/
7-
828
public final class SwapAdjacentBits {
929
private SwapAdjacentBits() {
1030
}
1131

32+
/**
33+
* Swaps every pair of adjacent bits of a given integer.
34+
* Steps:
35+
* 1. Mask the even-positioned bits.
36+
* 2. Mask the odd-positioned bits.
37+
* 3. Shift the even bits to the right and the odd bits to the left.
38+
* 4. Combine the shifted bits.
39+
*
40+
* @param num the integer whose bits are to be swapped
41+
* @return the integer after swapping every pair of adjacent bits
42+
*/
1243
public static int swapAdjacentBits(int num) {
1344
// mask the even bits (0xAAAAAAAA => 10101010...)
1445
int evenBits = num & 0xAAAAAAAA;

src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,20 @@
88
class SwapAdjacentBitsTest {
99

1010
@ParameterizedTest
11-
@CsvSource({
12-
"2, 1", // 2 (10 in binary) should become 1 (01 in binary)
13-
"43, 23", // 43 should become 23
14-
"153, 102", // 153 should become 102
15-
"15, 15", // 15 (1111) remains 15 (1111)
16-
"0, 0" // 0 (0000) remains 0 (0000)
17-
})
11+
@CsvSource({"2, 1", // 2 (binary: 10) -> 1 (binary: 01)
12+
"43, 23", // 43 (binary: 101011) -> 23 (binary: 010111)
13+
"153, 102", // 153 (binary: 10011001) -> 102 (binary: 01100110)
14+
"15, 15", // 15 (binary: 1111) -> 15 (binary: 1111) (no change)
15+
"0, 0", // 0 (binary: 0000) -> 0 (binary: 0000) (no change)
16+
"1, 2", // 1 (binary: 01) -> 2 (binary: 10)
17+
"170, 85", // 170 (binary: 10101010) -> 85 (binary: 01010101)
18+
"85, 170", // 85 (binary: 01010101) -> 170 (binary: 10101010)
19+
"255, 255", // 255 (binary: 11111111) -> 255 (binary: 11111111) (no change)
20+
"128, 64", // 128 (binary: 10000000) -> 64 (binary: 01000000)
21+
"1024, 2048",
22+
"-1, -1", // -1 (all bits 1) remains -1 (no change due to two's complement)
23+
"-2, -3", // -2 (binary: ...1110) -> -3 (binary: ...1101)
24+
"2147483647, -1073741825", "-2147483648, -1073741824"})
1825
void
1926
testSwapAdjacentBits(int input, int expected) {
2027
assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input));

0 commit comments

Comments
 (0)