Skip to content

Commit 6cbb599

Browse files
committed
feat(soobing): week8 > reverse-bits
1 parent baf6fe2 commit 6cbb599

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

โ€Žreverse-bits/soobing.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*
3+
* ๋ฌธ์ œ ์„ค๋ช…
4+
* - 32๋น„ํŠธ unsigned ์ •์ˆ˜์˜ ๋น„ํŠธ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์–ด์„œ ๋‹ค์‹œ ์ •์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ
5+
*
6+
* ์•„์ด๋””์–ด
7+
* - ๋น„ํŠธ ์—ฐ์‚ฐ์„ ์ดํ•ดํ•˜๊ณ , O(1)์•Œ์•„๊ฐ€๋Š”๋ฐ ์˜์˜๋ฅผ ๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Œ.
8+
*
9+
* ๋น„ํŠธ ์—ฐ์‚ฐ
10+
* >>>, >>, <<, &, |
11+
* - signed, unsigned ์—ฐ์‚ฐ
12+
*/
13+
function reverseBits(n: number): number {
14+
let result = 0;
15+
for (let i = 0; i < 32; i++) {
16+
result <<= 1;
17+
result |= n & 1;
18+
n >>>= 1;
19+
}
20+
21+
return result >>> 0;
22+
}

0 commit comments

Comments
ย (0)