Skip to content

Commit 39aa7a3

Browse files
author
Ram swaroop
committed
rightmost set bit done
1 parent 8f027c4 commit 39aa7a3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)