File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change 10
10
public class NextHigherNumber {
11
11
12
12
/**
13
+ * The main logic is to flip the bits in the first '01' bit pattern
14
+ * of {@param n} from the right and then push all 1 bits to the right
15
+ * of '01' to the extreme right.
16
+ *
17
+ *
18
+ * For example,
19
+ *
20
+ * 3 (0000011) = 5 (0000101)
21
+ * 6 (0000110) = 9 (0001001)
22
+ * 23 (0010111) = 27 (0011011)
23
+ * 24 (0011000) = 33 (0100001)
24
+ * 46 (0101110) = 51 (0110011)
25
+ *
13
26
* @param n
14
27
* @return
15
28
*/
@@ -31,10 +44,6 @@ public static int getNextHigherNumberWithSameSetBits(int n) {
31
44
n >>>= 1 ;
32
45
}
33
46
34
- /*System.out.println("n: " + Integer.toBinaryString(n << count));
35
- System.out.println("Left pattern: " + Integer.toBinaryString(leftPattern << count));
36
- System.out.println("Right pattern: " + Integer.toBinaryString(rightPattern >> 1));*/
37
-
38
47
return (n << count ) | (leftPattern << count ) | (rightPattern >> 1 );
39
48
}
40
49
You can’t perform that action at this time.
0 commit comments