File tree Expand file tree Collapse file tree 1 file changed +4
-1
lines changed
src/main/java/com/thealgorithms/bitmanipulation Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Original file line number Diff line number Diff line change @@ -17,12 +17,15 @@ private BitSwap() {
17
17
* @return The modified value with swapped bits
18
18
* @throws IllegalArgumentException if either position is negative or ≥ 32
19
19
*/
20
+
20
21
public static int bitSwap (int data , final int posA , final int posB ) {
21
22
if (posA < 0 || posA >= Integer .SIZE || posB < 0 || posB >= Integer .SIZE ) {
22
23
throw new IllegalArgumentException ("Bit positions must be between 0 and 31" );
23
24
}
24
25
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 ) {
26
29
data ^= (1 << posA ) ^ (1 << posB );
27
30
}
28
31
return data ;
You can’t perform that action at this time.
0 commit comments