Add parameterized tests for BitSwap (#6347)

This commit is contained in:
Oleksandr Klymenko
2025-07-06 19:03:02 +02:00
committed by GitHub
parent 9b4dec0b06
commit 350f149657
2 changed files with 73 additions and 9 deletions

View File

@@ -1,12 +1,27 @@
package com.thealgorithms.bitmanipulation;
/**
* Utility class for performing bit-swapping operations on integers.
* This class cannot be instantiated.
*/
public final class BitSwap {
private BitSwap() {
}
/*
* @brief Swaps the bits at the position posA and posB from data
/**
* Swaps two bits at specified positions in an integer.
*
* @param data The input integer whose bits need to be swapped
* @param posA The position of the first bit (0-based, from least significant)
* @param posB The position of the second bit (0-based, from least significant)
* @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)) {
data ^= (1 << posA) ^ (1 << posB);
}