Skip to content

Commit e131cf7

Browse files
committed
solve reverse bits
1 parent fc2ad73 commit e131cf7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

reverse-bits/sora0319.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
// you need treat n as an unsigned value
3+
public int reverseBits(int n) {
4+
String binary = String.format("%32s", Integer.toBinaryString(n)).replace(' ', '0');
5+
System.out.println(binary);
6+
7+
StringBuilder sb = new StringBuilder(binary);
8+
sb.reverse();
9+
10+
11+
return Integer.parseUnsignedInt(sb.toString(),2);
12+
}
13+
}
14+

0 commit comments

Comments
 (0)