We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5e9d9f7 commit 05ceb19Copy full SHA for 05ceb19
1 file changed
src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java
@@ -17,12 +17,15 @@ private BitSwap() {
17
* @return The modified value with swapped bits
18
* @throws IllegalArgumentException if either position is negative or ≥ 32
19
*/
20
+
21
public static int bitSwap(int data, final int posA, final int posB) {
22
if (posA < 0 || posA >= Integer.SIZE || posB < 0 || posB >= Integer.SIZE) {
23
throw new IllegalArgumentException("Bit positions must be between 0 and 31");
24
}
25
- if (SingleBitOperations.getBit(data, posA) != SingleBitOperations.getBit(data, posB)) {
26
+ boolean bitA = ((data >> posA) & 1) != 0;
27
+ boolean bitB = ((data >> posB) & 1) != 0;
28
+ if (bitA != bitB) {
29
data ^= (1 << posA) ^ (1 << posB);
30
31
return data;
0 commit comments