-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP4_2Variants.java
More file actions
30 lines (28 loc) · 1018 Bytes
/
P4_2Variants.java
File metadata and controls
30 lines (28 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class P4_2Variants
{
public static void main(String[] args) {
System.out.println(swapBits(0b1001, 1, 3) == 0b0011);
System.out.println(swapBits(0b1001, 1, 3) == 0b0011);
System.out.println(swapBits(0b10010000, 1, 7) == 0b00010010);
System.out.println(swapBits(0b10010000, 1, 1) == 0b10010000);
System.out.println(swapBits(0b10010010, 1, 4) == 0b10010010);
System.out.println(swapBits(0b10010010, 0, 4) == 0b10000011);
}
//Swap bits at indexs i and j
public static int swapBits(int x, int i, int j)
{
//Get the bit at indexes I and j
//If ~(i ^ j) == 0
//We need to switch the bits.
int sum = 0;
sum ^= (x & (1 << i)) >>> i;
sum ^= (x & (1 << j)) >>> j;
//Above 3 lines are redundanct.
//Would have been simpler to check if ((x >> i) & 1) != ((x >> j) & 1)
if(sum != 0)
{
x = (x ^ (1 << i)) ^ (1 << j);
}
return x;
}
}