-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path190.cpp
More file actions
42 lines (35 loc) · 778 Bytes
/
190.cpp
File metadata and controls
42 lines (35 loc) · 778 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
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Written by Nitin Kumar Maharana
* nitin.maharana@gmail.com
*/
class Solution {
public:
uint32_t reverseBits1(uint32_t n) {
uint32_t result = 0;
for(int i = 0; i < 32; i++)
{
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
uint32_t reverseBits(uint32_t n) {
uint32_t l, r, temp, shift;
l = 1;
r = 1 << 31;
shift = 31;
while(l < r)
{
temp = (n & l) << shift;
n = n & ~l;
n = n | ((n & r) >> shift);
n = n & ~r;
n = n | temp;
l <<= 1;
r >>= 1;
shift -= 2;
}
return n;
}
};