From 7aeb2dfdb6da9e6d3f3be8037276185de47de9ba Mon Sep 17 00:00:00 2001 From: MahaleHarsh <96824988+MahaleHarsh@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:31:43 +0530 Subject: [PATCH] Update BitSwap.java removed the external getBit() method and replaced it with direct bitwise operations to check and swap bits more efficiently. --- .../java/com/thealgorithms/bitmanipulation/BitSwap.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java b/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java index 40b3097b1276..098a5ba9108d 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java @@ -7,8 +7,10 @@ private BitSwap() { * @brief Swaps the bits at the position posA and posB from data */ public static int bitSwap(int data, final int posA, final int posB) { - if (SingleBitOperations.getBit(data, posA) != SingleBitOperations.getBit(data, posB)) { - data ^= (1 << posA) ^ (1 << posB); + // Check if the bits at posA and posB are different + if (((data >> posA) & 1) != ((data >> posB) & 1)) { + // Swap the bits using XOR + data ^= (1 << posA) | (1 << posB); } return data; }