-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerofTwo.hpp
66 lines (54 loc) · 1.07 KB
/
PowerofTwo.hpp
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
59
60
61
62
63
64
65
66
//Power of Two
//
//Given an integer, write a function to determine if it is a power of two.
#include <vector>
class Solution
{
public:
bool isPowerOfTwo(int n)
{
return (n > 0) && (n & n - 1) == 0;
}
bool isPowerOfTwo2(int n)
{
if (n <= 0)
return false;
std::size_t cnt = 0;
while (n)
{
n &= n - 1;
if (++cnt > 1)
return false;
}
if (1 == cnt)
return true;
}
bool isPowerOfTwo1(int n)
{
if (n <= 0)
return false;
static int weight[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
, 4096, 8192, 16384, 32768, 65536, 131072, 262144
, 524288, 1048576, 2097152, 4194304, 8388608, 16777216
, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824 };
if (n > weight[30])
return false;
std::size_t l = 0;
std::size_t e = sizeof(weight) / sizeof(int) - 1;
while (l < e)
{
std::size_t m = (l + e) / 2;
if (weight[m] == n)
return true;
else if (weight[m] < n)
l = m + 1;
else if (weight[m] > n)
e = m - 1;
}
// l==e
if (weight[l] == n)
return true;
else
return false;
}
};