From def7a0b0879a5965707d2e47c21cee5b9946ca5e Mon Sep 17 00:00:00 2001 From: DarkMatter-999 Date: Tue, 8 Oct 2024 11:56:51 +0530 Subject: [PATCH 1/3] Added algorithm for Swaping Adjacent Bits using Bit Manipulation --- .../bitmanipulation/SwapAdjacentBits.java | 26 +++++++++++++++++++ .../bitmanipulation/SwapAdjacentBitsTest.java | 21 +++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java new file mode 100644 index 000000000000..933dec5654a0 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java @@ -0,0 +1,26 @@ +package com.thealgorithms.bitmanipulation; + +/** + * Swap every pair of adjacent bits of a given number. + * @author Lakshyajeet Singh Goyal (https://github.com/DarkMatter-999) + */ + +public final class SwapAdjacentBits { + private SwapAdjacentBits() { + } + + 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 new file mode 100644 index 000000000000..ef920acd7e9e --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java @@ -0,0 +1,21 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class SwapAdjacentBitsTest { + + @Test + void testSwapAdjacentBits() { + assertEquals(1, SwapAdjacentBits.swapAdjacentBits(2)); + + assertEquals(23, SwapAdjacentBits.swapAdjacentBits(43)); + + assertEquals(102, SwapAdjacentBits.swapAdjacentBits(153)); + + assertEquals(15, SwapAdjacentBits.swapAdjacentBits(15)); + + assertEquals(0, SwapAdjacentBits.swapAdjacentBits(0)); + } +} From b46a1b4059d96cef39dd099bc352a746b131c5e9 Mon Sep 17 00:00:00 2001 From: DarkMatter-999 Date: Wed, 9 Oct 2024 09:28:18 +0530 Subject: [PATCH 2/3] added parameterized tests --- .../bitmanipulation/SwapAdjacentBitsTest.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java index ef920acd7e9e..247cb1f98550 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java @@ -2,20 +2,20 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; class SwapAdjacentBitsTest { - @Test - void testSwapAdjacentBits() { - assertEquals(1, SwapAdjacentBits.swapAdjacentBits(2)); - - assertEquals(23, SwapAdjacentBits.swapAdjacentBits(43)); - - assertEquals(102, SwapAdjacentBits.swapAdjacentBits(153)); - - assertEquals(15, SwapAdjacentBits.swapAdjacentBits(15)); - - assertEquals(0, SwapAdjacentBits.swapAdjacentBits(0)); + @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) + }) + void testSwapAdjacentBits(int input, int expected) { + assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input)); } } From ffb0ed84d2bbb7e3242c04e20a7a1b558b244b5d Mon Sep 17 00:00:00 2001 From: DarkMatter-999 Date: Wed, 9 Oct 2024 09:31:21 +0530 Subject: [PATCH 3/3] clang format fixed --- .../bitmanipulation/SwapAdjacentBitsTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java index 247cb1f98550..67c986136ab0 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java @@ -9,13 +9,14 @@ class SwapAdjacentBitsTest { @ParameterizedTest @CsvSource({ - "2, 1", // 2 (10 in binary) should become 1 (01 in binary) - "43, 23", // 43 should become 23 + "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) + "15, 15", // 15 (1111) remains 15 (1111) + "0, 0" // 0 (0000) remains 0 (0000) }) - void testSwapAdjacentBits(int input, int expected) { + void + testSwapAdjacentBits(int input, int expected) { assertEquals(expected, SwapAdjacentBits.swapAdjacentBits(input)); } }