Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ private BitSwap() {
* @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)) {
boolean bitA = ((data >> posA) & 1) != 0;
boolean bitB = ((data >> posB) & 1) != 0;
if (bitA != bitB) {
data ^= (1 << posA) ^ (1 << posB);
}
return data;
Expand Down