mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Bit swap enhancement (#6577)
This commit is contained in:
committed by
GitHub
parent
5e9d9f7337
commit
05ceb192c9
@@ -17,12 +17,15 @@ public final class 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;
|
||||
|
||||
Reference in New Issue
Block a user