forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.go
More file actions
29 lines (26 loc) ยท 668 Bytes
/
obzva.go
File metadata and controls
29 lines (26 loc) ยท 668 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
ํ์ด
- ์๋ณธ uint32 num์ ๋ํ์ฌ LSB๋ถํฐ(๊ฐ์ฅ ์ค๋ฅธ์ชฝ bit) ํ์ํฉ๋๋ค
LSB % 2 == 1 -> uint32์ ์๋ก์ด MSB์(๊ฐ์ฅ ์ผ์ชฝ bit) 1 ์ถ๊ฐ
else -> 0 ์ถ๊ฐ
Big O
- Time complexity: O(1)
- input num์ ์๊ด ์์ด 32๋ฒ์ ๋ฐ๋ณต์ ๊ณ ์ ์ ์ผ๋ก ์คํํฉ๋๋ค
- Space complexity: O(1)
*/
func reverseBits(num uint32) uint32 {
var res uint32 = 0
for i := 0; i < 32; i++ {
// using numerical operators
// if num % 2 == 1 {
// res = res * 2 + 1
// } else {
// res *= 2
// }
// num /= 2
// using bitwise operators
res = (res << 1) | (num & 1)
num >>= 1
}
return res
}