We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e499d3b commit 7aeb2dfCopy full SHA for 7aeb2df
src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java
@@ -7,8 +7,10 @@ private BitSwap() {
7
* @brief Swaps the bits at the position posA and posB from data
8
*/
9
public static int bitSwap(int data, final int posA, final int posB) {
10
- if (SingleBitOperations.getBit(data, posA) != SingleBitOperations.getBit(data, posB)) {
11
- data ^= (1 << posA) ^ (1 << posB);
+ // Check if the bits at posA and posB are different
+ if (((data >> posA) & 1) != ((data >> posB) & 1)) {
12
+ // Swap the bits using XOR
13
+ data ^= (1 << posA) | (1 << posB);
14
}
15
return data;
16
0 commit comments