Skip to content

Commit

Permalink
feat:Reverse Bits #234
Browse files Browse the repository at this point in the history
  • Loading branch information
donghyeon95 authored and donghyeon95 committed Dec 27, 2024
1 parent b7f8de2 commit fc875c4
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions reverse-bits/donghyeon95.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int result = 0;

for (int i = 0; i < 32; i++) {
// 왼쪽으로 비트를 한 칸 이동하고, n의 마지막 비트를 추가
result = (result << 1) | (n & 1);
// n을 오른쪽으로 한 칸 이동
n >>= 1;
}

return result;
}
}

0 comments on commit fc875c4

Please sign in to comment.