-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_bits.cpp
58 lines (54 loc) · 1.31 KB
/
reverse_bits.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// =====================================================================================
//
// Filename: reverse_bits.cpp
//
// Description: 190. Reverse Bits.
//
// Version: 1.0
// Created: 08/21/2019 07:58:47 PM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <stdio.h>
#include <stdint.h>
// Use extra variable
class Solution1
{
public:
uint32_t reverseBits(uint32_t n)
{
int m = 0;
for (int i = 0; i < 32; i++)
{
m = (m << 1) | (n & 0x1);
n >>= 1;
}
return m;
}
};
// Binary search
class Solution2
{
public:
uint32_t reverseBits(uint32_t n)
{
n = (n >> 16) | (n << 16);
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4);
n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2);
n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1);
return n;
}
};
using Solution = Solution2;
int main(int argc, char* argv[])
{
uint32_t n = 43261596;
uint32_t m = Solution().reverseBits(n);
printf("%d -> %d\n", n, m);
return 0;
}