-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextMinMax.java
More file actions
60 lines (47 loc) · 1.19 KB
/
NextMinMax.java
File metadata and controls
60 lines (47 loc) · 1.19 KB
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
package bit;
public class NextMinMax {
/*
* Get the next max and min numbers with the same number of bit counts for 1
* or 0.
*/
public static void printNextMaxAndMin(int n) {
int a = n, b = n;
int firstZero = 0, nextOne = 0, firstOne = 0, nextZero = 0;
while (a != 0) {
if (a % 2 == 0)
break;
firstZero++;
a = a >> 1;
}
while (a != 0) {
if (a % 2 == 1)
break;
nextOne++;
a = a >> 1;
}
nextOne = nextOne + firstZero;
while (b != 0) {
if (b % 2 == 1)
break;
firstOne++;
b = b >> 1;
}
while (b != 0) {
if (b % 2 == 0)
break;
nextZero++;
b = b >> 1;
}
nextZero = nextZero + firstOne;
System.out.println("Next Max : " + Integer.toBinaryString(swapBits(n, firstOne, nextZero)));
System.out.println("Next Min : " + Integer.toBinaryString(swapBits(n, nextOne, firstZero)));
}
public static int swapBits(int n, int one, int zero) {
int oneValue = (int) Math.pow(2, one);
int zeroValue = (int) Math.pow(2, zero);
return n - oneValue + zeroValue;
}
public static void main(String[] args) {
NextMinMax.printNextMaxAndMin(Integer.valueOf("101", 2));
}
}