Repository navigation
Expand file tree
/
Copy pathbits.cc
More file actions
208 lines (171 loc) · 3.35 KB
/
Copy pathbits.cc
File metadata and controls
208 lines (171 loc) · 3.35 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <vector>
int getNumSetBits(int num)
{
int cnt = 0;
while (num) {
num &= num - 1;
cnt++;
}
return cnt;
}
int getNumSetBitsFast(int num)
{
int numBits[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 3, 3, 2, 3, 3, 4};
int cnt = 0;
while (num) {
cnt += numBits[num & 0xF];
num >>= 4;
}
return cnt;
}
bool isOddParity(int num)
{
return (getNumSetBits(num) & 1) ? true : false;
}
bool isEvenParity(int num)
{
return (getNumSetBits(num) & 1) ? false : true;
}
void setBit(int& num, int bit)
{
num |= (1 << bit);
}
void clearBit(int& num, int bit)
{
num &= ~(1 << bit);
}
bool isBitClear(int num, int bit)
{
return (((num >> bit) & 1) == 0);
}
unsigned char rotate(unsigned char s, int n)
{
return (s >> n) | (s << (8 - n));
}
bool isBitSet(int num, int bit)
{
return (num & (1 << bit));
}
bool isPalindrome(int num)
{
for (int i = 0, j = sizeof(num) - 1; i < j; i++, j--) {
if (isBitSet(num, i) != isBitSet(num, j)) {
return false;
}
}
return true;
}
bool isBitSet(const char* data, int bit)
{
return data[bit / 8] & (1 << bit % 8);
}
bool isPalindrome(const char* data, int numBits)
{
if ((data == nullptr) || (numBits == 0)) {
return false;
}
for (int i = 0, j = numBits - 1; i < j; i++, j--) {
if (isBitSet(data, i) != isBitSet(data, j)) {
return false;
}
}
return true;
}
static int getNumTrailingZeros(int num)
{
int cnt = 0;
while ((num & 1) == 0) {
cnt++;
num >>= 1;
}
return cnt;
}
static int getNumTrailingOnes(int num)
{
int cnt = 0;
while ((num & 1) == 1) {
cnt++;
num >>= 1;
}
return cnt;
}
int getNextNumWithSameOnes(int num)
{
if (num <= 0) {
return -1;
}
int numTrailingZeros = getNumTrailingZeros(num);
int numTrailingOnes = getNumTrailingOnes(num >> numTrailingZeros);
int i = numTrailingZeros + numTrailingOnes;
num |= 1 << i;
i--;
while (i >= (numTrailingOnes - 1)) {
num &= ~(1 << i);
i--;
}
while (i >= 0) {
num |= (1 << i);
i--;
}
return num;
}
int getPrevNumWithSameOnes(int num)
{
if (num <= 0) {
return -1;
}
int numTrailingOnes = getNumTrailingOnes(num);
int numTrailingZeros = getNumTrailingZeros(num >> numTrailingOnes);
int i = numTrailingZeros + numTrailingOnes;
num &= ~(1 << i);
i--;
while (i >= (numTrailingZeros - 1)) {
num |= (1 << i);
i--;
}
while (i >= 0) {
num &= ~(1 << i);
i--;
}
return num;
}
int numBytesUtf8(char data)
{
int cnt = 0;
while (data & 0x80) {
cnt++;
data <<= 1;
}
return cnt == 0 ? 1 : cnt;
}
/*
* A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
* For 1-byte character, the first bit is a 0, followed by its unicode code.
* For n-bytes character, the first n-bits are all one's, the n+1 bit is 0, followed by n-1
* bytes with most significant 2 bits being 10.
*/
bool isValidUtf8(const std::vector<char>& vec)
{
if (vec.empty()) {
return false;
}
int sz = vec.size();
for (int i = 0; i < sz;) {
if ((vec[i] >> 6) == 2) {
return false;
}
int numBytes = numBytesUtf8(vec[i]);
if (i + numBytes > sz || numBytes > 4) {
return false;
}
for (int j = i + 1; j < i + numBytes; j++) {
if ((vec[j] & 0x80) != 0x80) {
return false;
}
}
i += numBytes;
}
return true;
}
int main() {}