Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 81092c4

Browse files
author
Ram swaroop
committed
next higher no. done
1 parent 5f8021c commit 81092c4

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/me/ramswaroop/bits/NextHigherNumber.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
public class NextHigherNumber {
1111

1212
/**
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+
*
1326
* @param n
1427
* @return
1528
*/
@@ -31,10 +44,6 @@ public static int getNextHigherNumberWithSameSetBits(int n) {
3144
n >>>= 1;
3245
}
3346

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-
3847
return (n << count) | (leftPattern << count) | (rightPattern >> 1);
3948
}
4049

0 commit comments

Comments
 (0)