File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ package me .ramswaroop .bits ;
2
+
3
+ /**
4
+ * Created by IntelliJ IDEA.
5
+ *
6
+ * @author: ramswaroop
7
+ * @date: 6/3/15
8
+ * @time: 11:56 PM
9
+ */
10
+ public class RightmostSetBit {
11
+
12
+ public static int getRightmostSetBitPosition (long n ) {
13
+ int position = 0 ;
14
+ while (n > 0 ) {
15
+ position ++;
16
+ if ((n & 1 ) == 1 ) {
17
+ break ;
18
+ }
19
+ n >>= 1 ;
20
+ }
21
+ return position ;
22
+ }
23
+
24
+ public static void main (String a []) {
25
+ System .out .println (getRightmostSetBitPosition (0 ));
26
+ System .out .println (getRightmostSetBitPosition (1 ));
27
+ System .out .println (getRightmostSetBitPosition (2 ));
28
+ System .out .println (getRightmostSetBitPosition (5 ));
29
+ System .out .println (getRightmostSetBitPosition (18 ));
30
+ System .out .println (getRightmostSetBitPosition (19 ));
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments