Skip to content

Commit fc875c4

Browse files
donghyeon95donghyeon95
authored andcommitted
feat:Reverse Bits DaleStudy#234
1 parent b7f8de2 commit fc875c4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

reverse-bits/donghyeon95.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
// you need treat n as an unsigned value
3+
public int reverseBits(int n) {
4+
int result = 0;
5+
6+
for (int i = 0; i < 32; i++) {
7+
// 왼쪽으로 비트를 한 칸 이동하고, n의 마지막 비트를 추가
8+
result = (result << 1) | (n & 1);
9+
// n을 오른쪽으로 한 칸 이동
10+
n >>= 1;
11+
}
12+
13+
return result;
14+
}
15+
}
16+

0 commit comments

Comments
 (0)