We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 76fb734 commit a4ed5d3Copy full SHA for a4ed5d3
โreverse-bits/flynn.goโ
@@ -0,0 +1,29 @@
1
+/*
2
+ํ์ด
3
+- ์๋ณธ uint32 num์ ๋ํ์ฌ LSB๋ถํฐ(๊ฐ์ฅ ์ค๋ฅธ์ชฝ bit) ํ์ํฉ๋๋ค
4
+ LSB % 2 == 1 -> uint32์ ์๋ก์ด MSB์(๊ฐ์ฅ ์ผ์ชฝ bit) 1 ์ถ๊ฐ
5
+ else -> 0 ์ถ๊ฐ
6
+Big O
7
+- Time complexity: O(1)
8
+ - input num์ ์๊ด ์์ด 32๋ฒ์ ๋ฐ๋ณต์ ๊ณ ์ ์ ์ผ๋ก ์คํํฉ๋๋ค
9
+- Space complexity: O(1)
10
+*/
11
+
12
+func reverseBits(num uint32) uint32 {
13
+ var res uint32 = 0
14
+ for i := 0; i < 32; i++ {
15
+ // using numerical operators
16
+ // if num % 2 == 1 {
17
+ // res = res * 2 + 1
18
+ // } else {
19
+ // res *= 2
20
+ // }
21
+ // num /= 2
22
23
+ // using bitwise operators
24
+ res = (res << 1) | (num & 1)
25
+ num >>= 1
26
+ }
27
28
+ return res
29
+}
0 commit comments