Skip to content

Commit 5747260

Browse files
committed
reverse bits solved
1 parent 79627ed commit 5747260

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

โ€Žreverse-bits/mintheon.javaโ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
/**
3+
์‹œ๊ฐ„๋ณต์žก๋„: O(1) -> ๋ฃจํ”„๋Š” ํ•ญ์ƒ 32๋ฒˆ ๋ฐ˜๋ณต๋˜๊ธฐ ๋•Œ๋ฌธ
4+
๊ณต๊ฐ„๋ณต์žก๋„: O(1)
5+
*/
6+
7+
// you need treat n as an unsigned value
8+
public int reverseBits(int n) {
9+
int answer = 0;
10+
int index = 31;
11+
12+
while(n != 0) {
13+
// n&1 : ๋งˆ์ง€๋ง‰ ๋น„ํŠธ๋ฅผ ์ถ”์ถœ
14+
// << : 0์„ ํŒจ๋”ฉ์ฒ˜๋ฆฌ ์‹œ์ผœ์„œ ์ƒ์œ„ ์ž๋ฆฌ์ˆ˜๋กœ ์˜ฌ๋ ค๋ฒ„๋ฆผ
15+
answer += (n & 1) << index;
16+
17+
// >>> : ๋ถ€ํ˜ธ ์ƒ๊ด€์—†์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋น„ํŠธ ์ด๋™
18+
n = n >>> 1;
19+
index--;
20+
}
21+
22+
return answer;
23+
}
24+
}

0 commit comments

Comments
ย (0)