-
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
1 parent
b6138ed
commit 8681b04
Showing
1 changed file
with
24 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,24 @@ | ||
/* | ||
runtime 0 ms, beats 100.00% | ||
memory 41.47 MB, beats 90.14% | ||
time complexity: O(1) | ||
space complexity: O(1) | ||
i번째 bit를 구하고, 이를 ans(초기값 0)의 31-i번째 자리에 bitwise-OR 연산을 하여, bit 위치를 reverse 했습니다. | ||
*/ | ||
|
||
public class Solution { | ||
// you need treat n as an unsigned value | ||
public int reverseBits(int n) { | ||
int x = 1; | ||
int ans = 0; | ||
for (int i = 0; i < 32; i++) { | ||
int bit = (x & n) >>> i; | ||
bit <<= (31 - i); | ||
ans |= bit; | ||
x <<= 1; | ||
} | ||
return ans; | ||
} | ||
} |