Skip to content

Commit 48ae88f

Browse files
lukasb1bvil02
andauthored
* Create BitSwap.java * Create BitSwapTest.java * Update BitSwap.java * Update BitSwap.java * Update BitSwapTest.java * Update BitSwap.java * Update BitSwap.java * Update BitSwapTest.java * Update BitSwapTest.java * Update src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update BitSwap.java * Update BitSwap.java * Update BitSwap.java * Update src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * Update src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> * style: remove redundant blank line --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
1 parent 24a8223 commit 48ae88f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
public final class BitSwap {
4+
private BitSwap() {
5+
}
6+
/*
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);
12+
}
13+
return data;
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
public class BitSwapTest {
7+
@Test
8+
void testHighestSetBit() {
9+
assertEquals(3, BitSwap.bitSwap(3, 0, 1));
10+
assertEquals(5, BitSwap.bitSwap(6, 0, 1));
11+
assertEquals(7, BitSwap.bitSwap(7, 1, 1));
12+
}
13+
}

0 commit comments

Comments
 (0)