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() {
1717 * @return The modified value with swapped bits
1818 * @throws IllegalArgumentException if either position is negative or ≥ 32
1919 */
20+
2021 public static int bitSwap (int data , final int posA , final int posB ) {
2122 if (posA < 0 || posA >= Integer .SIZE || posB < 0 || posB >= Integer .SIZE ) {
2223 throw new IllegalArgumentException ("Bit positions must be between 0 and 31" );
2324 }
2425
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 ) {
2629 data ^= (1 << posA ) ^ (1 << posB );
2730 }
2831 return data ;
You can’t perform that action at this time.
0 commit comments