-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
85 lines (77 loc) · 1.99 KB
/
binary.cpp
File metadata and controls
85 lines (77 loc) · 1.99 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include"binary.h"
namespace myLib
{
/**************************************************
* * @file binary.cpp
* * @author EncodedStar
* * @date 2019.7.3
* * @function 计算n二进制的时候存在几个1
***************************************************/
int count_binary(int n) {
int res = 0;
while (n != 0) {
n = n & (n - 1);
res++;
}
return res;
}
/**************************************************
* * @file binary.cpp
* * @author EncodedStar
* * @date 2019.7.5
* * @function 16进制打印
***************************************************/
std::string toHex(int num) {
if (num == 0) return "0";
std::string hex = "0123456789abcdef", ans = "";
while(num && ans.size() < 8){
ans = hex[num & 0xf] + ans;
num >>= 4;
}
return ans;
}
/**************************************************
* * @file binary.cpp
* * @author EncodedStar
* * @date 2019.7.5
* * @function int转化string and vector<string>
***************************************************/
std::string int2String(int n) {
std::stringstream tmpss;
std::string tmps;
tmpss.clear();
tmpss << n;
tmpss >> tmps;
tmpss.str("");
return tmps;
}
vector<string> int2VString(int n) {
vector<string> tmpv;
for(int i = 1; i <= n; i++)
{
tmpv.push_back(int2String(i));
}
return tmpv;
}
/**************************************************
* * @file binary.cpp
* * @author EncodedStar
* * @date 2020.3.23
* * @function 开平方
***************************************************/
float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// 2nd iteration, this can be removed
// // y = y * ( threehalfs - ( x2 * y * y ) );
return y;
}
}