From c15b9662cce58018addcdba267749c308ff738d4 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 16 Oct 2024 08:56:20 +0530 Subject: [PATCH 1/2] refactor: Enhance docs, add more tests in `SwapAdjacentBits` --- .../bitmanipulation/SwapAdjacentBits.java | 44 ++++++++++++++----- .../bitmanipulation/SwapAdjacentBitsTest.java | 21 ++++++--- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java index 933dec5654a0..cffd0fbb6134 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java @@ -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; } } diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java index 67c986136ab0..12f0542b92f6 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java @@ -8,13 +8,20 @@ class SwapAdjacentBitsTest { @ParameterizedTest - @CsvSource({ - "2, 1", // 2 (10 in binary) should become 1 (01 in binary) - "43, 23", // 43 should become 23 - "153, 102", // 153 should become 102 - "15, 15", // 15 (1111) remains 15 (1111) - "0, 0" // 0 (0000) remains 0 (0000) - }) + @CsvSource({"2, 1", // 2 (binary: 10) -> 1 (binary: 01) + "43, 23", // 43 (binary: 101011) -> 23 (binary: 010111) + "153, 102", // 153 (binary: 10011001) -> 102 (binary: 01100110) + "15, 15", // 15 (binary: 1111) -> 15 (binary: 1111) (no change) + "0, 0", // 0 (binary: 0000) -> 0 (binary: 0000) (no change) + "1, 2", // 1 (binary: 01) -> 2 (binary: 10) + "170, 85", // 170 (binary: 10101010) -> 85 (binary: 01010101) + "85, 170", // 85 (binary: 01010101) -> 170 (binary: 10101010) + "255, 255", // 255 (binary: 11111111) -> 255 (binary: 11111111) (no change) + "128, 64", // 128 (binary: 10000000) -> 64 (binary: 01000000) + "1024, 2048", + "-1, -1", // -1 (all bits 1) remains -1 (no change due to two's complement) + "-2, -3", // -2 (binary: ...1110) -> -3 (binary: ...1101) + "2147483647, -1073741825", "-2147483648, -1073741824"}) void testSwapAdjacentBits(int input, int expected) { assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input)); From e1c568f9e96349504f88dfacbb7190dadfdb2198 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Tue, 29 Oct 2024 10:06:07 +0530 Subject: [PATCH 2/2] Fix --- .../thealgorithms/bitmanipulation/SwapAdjacentBits.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java index cffd0fbb6134..98a7de8bdf1a 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java @@ -41,10 +41,17 @@ private SwapAdjacentBits() { * @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; } }