forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapBits.java
89 lines (80 loc) · 2.7 KB
/
SwapBits.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.rampatra.bits;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/13/15
* @time: 4:45 PM
*/
public class SwapBits {
/**
* Swaps bits at even position with bits
* at odd position in {@param n}.
*
* @param n
* @return
*/
public static int swapEvenOddBits(int n) {
int evenBits = n & 0x55555555;
int oddBits = n & 0xaaaaaaaa;
return evenBits << 1 | oddBits >> 1;
}
/**
* Swaps bits at even position with bits
* at odd position in {@param n}.
*
* @param n
* @return
*/
public static int swapEvenOddBits_V1(int n) {
for (int i = 0; i < 32; i += 2) {
int evenBit = (n >> i) & 1;
int oddBit = (n >> (i + 1)) & 1;
int xor = evenBit ^ oddBit;
n ^= xor << i;
n ^= xor << (i + 1);
}
return n;
}
/**
* Swaps {@param length} bits in {@param n} starting from
* {@param pos1} with bits starting from {@param pos2}.
* <p/>
* For example,
* x = 28 (11100)
* p1 = 0 (Start from first bit from right side)
* p2 = 3 (Start from 4th bit from right side)
* l = 2 (No of bits to be swapped)
* Output:
* 7 (00111)
*
* @param n
* @param pos1 starts from 0
* @param pos2 starts from 0
* @param length
* @return
*/
public static int swapBitRangeInNumber(int n, int pos1, int pos2, int length) {
int set1 = (n >> pos1) & ((1 << length) - 1); // 1st set of bits to be swapped
int set2 = (n >> pos2) & ((1 << length) - 1); // 2nd set of bits to be swapped
int xor = set1 ^ set2; // difference pattern between the bits to be swapped
return n ^ (xor << pos1) ^ (xor << pos2); // XORing the difference pattern at the appropriate
// position of the bits to be swapped gives us the result
// (this logic is similar to swapping bits using XOR)
}
public static void main(String[] args) {
System.out.println(swapEvenOddBits(23));
System.out.println(swapEvenOddBits(0));
System.out.println(swapEvenOddBits(5));
System.out.println(swapEvenOddBits(6));
System.out.println("-------------------------------");
System.out.println(swapEvenOddBits_V1(23));
System.out.println(swapEvenOddBits_V1(0));
System.out.println(swapEvenOddBits_V1(5));
System.out.println(swapEvenOddBits_V1(6));
System.out.println("-------------------------------");
System.out.println(swapBitRangeInNumber(47, 1, 5, 3));
System.out.println(swapBitRangeInNumber(28, 0, 3, 2));
System.out.println(swapBitRangeInNumber(269, 1, 3, 2));
}
}