-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|