From 4d1087f01c8d95cf257e1092260972d0c668ff80 Mon Sep 17 00:00:00 2001 From: Kunwar Partap Singh <116331466+kunwarbhattal@users.noreply.github.com> Date: Wed, 1 Oct 2025 01:00:14 +0530 Subject: [PATCH 1/2] enhance bitSwap --- src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java b/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java index d8c207567ba6..d56c8985751f 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java @@ -17,12 +17,15 @@ private BitSwap() { * @return The modified value with swapped bits * @throws IllegalArgumentException if either position is negative or ≥ 32 */ + public static int bitSwap(int data, final int posA, final int posB) { if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) { throw new IllegalArgumentException("Bit positions must be between 0 and 31"); } - if (SingleBitOperations.getBit(data, posA) != SingleBitOperations.getBit(data, posB)) { + boolean bitA = ((data >> posA) & 1) != 0; + boolean bitB = ((data >> posB) & 1) != 0; + if (bitA != bitB) { data ^= (1 << posA) ^ (1 << posB); } return data; From d0adbc38f04ebaa45c01f40ce1f8f5f0d8eeb640 Mon Sep 17 00:00:00 2001 From: Kunwar Partap Singh <116331466+kunwarbhattal@users.noreply.github.com> Date: Wed, 1 Oct 2025 21:29:55 +0530 Subject: [PATCH 2/2] Update BitSwap.java --- src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java b/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java index d56c8985751f..634c9e7b3b44 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java @@ -17,7 +17,7 @@ private BitSwap() { * @return The modified value with swapped bits * @throws IllegalArgumentException if either position is negative or ≥ 32 */ - + public static int bitSwap(int data, final int posA, final int posB) { if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) { throw new IllegalArgumentException("Bit positions must be between 0 and 31");