-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigInt.cpp
More file actions
256 lines (199 loc) · 5.4 KB
/
BigInt.cpp
File metadata and controls
256 lines (199 loc) · 5.4 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "types.h"
#define MAX_BLOCKS 1024
// 大整数结构体
struct BigInt
{
int len; // 实际使用的块数(最低有效位在d[0])
u32 d[MAX_BLOCKS];
};
// 辅助函数:判断大整数是否为0
static int isZero(const struct BigInt *a)
{
for (int i = 0; i < a->len; i++)
{
if (a->d[i] != 0)
return 0;
}
return 1;
}
// 辅助函数:重新计算大整数的实际长度
static void normalize(struct BigInt *a)
{
while (a->len > 0 && a->d[a->len - 1] == 0)
{
a->len--;
}
if (a->len == 0)
{
a->len = 1; // 保持至少一个块
a->d[0] = 0;
}
}
// 1. 按位与运算
void bigint_and(struct BigInt *result, const struct BigInt *a, const struct BigInt *b)
{
int min_len = std::min(a->len, b->len);
// 结果长度取较小值(因为高位与0与运算结果为0)
result->len = min_len;
// 对重叠部分进行与运算
for (int i = 0; i < min_len; i++)
{
result->d[i] = a->d[i] & b->d[i];
}
// 处理较短大整数的高位(默认为0)
normalize(result);
}
// 2. 按位或运算
void bigint_or(struct BigInt *result, const struct BigInt *a, const struct BigInt *b)
{
int min_len = std::min(a->len, b->len);
int max_len = std::max(a->len, b->len);
// 结果长度取较大值
result->len = max_len;
// 对重叠部分进行或运算
for (int i = 0; i < min_len; i++)
{
result->d[i] = a->d[i] | b->d[i];
}
// 处理较长部分的高位
if (a->len > b->len)
{
for (int i = min_len; i < max_len; i++)
{
result->d[i] = a->d[i];
}
}
else if (b->len > a->len)
{
for (int i = min_len; i < max_len; i++)
{
result->d[i] = b->d[i];
}
}
normalize(result);
}
// 3. 按位异或运算
void bigint_xor(struct BigInt *result, const struct BigInt *a, const struct BigInt *b)
{
int min_len = std::min(a->len, b->len);
int max_len = std::max(a->len, b->len);
// 结果长度取较大值
result->len = max_len;
// 对重叠部分进行异或运算
for (int i = 0; i < min_len; i++)
{
result->d[i] = a->d[i] ^ b->d[i];
}
// 处理较长部分的高位
if (a->len > b->len)
{
for (int i = min_len; i < max_len; i++)
{
result->d[i] = a->d[i];
}
}
else if (b->len > a->len)
{
for (int i = min_len; i < max_len; i++)
{
result->d[i] = b->d[i];
}
}
normalize(result);
}
void u32_write_bits(u32 *dest, const u32 *src, int start_bit, int end_bit)
{
// if (start_bit < 0 || end_bit < start_bit) return *dest;
int len = end_bit - start_bit + 1;
u32 mask;
if (len == 32)
mask = 0xFFFFFFFF;
else
mask = ((1u << len) - 1u) << start_bit;
// 清除目标位
*dest &= ~mask;
// 设置新值
u32 value;
if (len == 32)
value = *src;
else
value = (*src & ((1u << len) - 1u)) << start_bit;
*dest |= value;
return;
}
u32 u32_read_bits(const u32 *src, int start_bit, int end_bit)
{
// if (start_bit < 0 || end_bit < start_bit) return 0;
int len = end_bit - start_bit + 1;
u32 mask;
if (len == 32)
mask = 0xFFFFFFFF;
else
mask = ((1u << len) - 1u) << start_bit;
u32 value = (*src & mask) >> start_bit;
return value;
}
// 按位写入
void bigint_write_bits(struct BigInt *dest, const u32 *src, int start_bit, int end_bit)
{
// if (start_bit < 0 || end_bit < start_bit) return;
int len = end_bit - start_bit + 1;
int block = start_bit / 32;
int off = start_bit % 32;
if (off + len <= 32)
{
// u32 old = dest->d[block];
u32_write_bits(&dest->d[block], src, off, off + len - 1);
// if (ret == old) return;
if (block + 1 > dest->len)
dest->len = block + 1;
return;
}
int first_bits = 32 - off;
int second_bits = len - first_bits;
u32 low_part = u32_read_bits(src, 0, first_bits - 1);
// u32 old1 = dest->d[block];
u32_write_bits(&dest->d[block], &low_part, off, 31);
// if (ret1 == old1) return;
u32 high_part = u32_read_bits(src, first_bits, len - 1);
// u32 old2 = dest->d[block + 1];
u32_write_bits(&dest->d[block + 1], &high_part, 0, second_bits - 1);
// if (ret2 == old2) return;
if (block + 2 > dest->len)
dest->len = block + 2;
return;
}
// 按位读出
u32 bigint_read_bits(const BigInt *a,
int start_bit,
int end_bit)
{
// if (start_bit < 0 || end_bit < start_bit) return 0;
int len = end_bit - start_bit + 1; // <= 32
int block = start_bit / 32;
int off = start_bit % 32;
if (off + len <= 32)
{
return u32_read_bits(&a->d[block], off, off + len - 1);
}
int first_bits = 32 - off;
int second_bits = len - first_bits;
u32 low_part = u32_read_bits(&a->d[block],
off,
31);
u32 high_part = u32_read_bits(&a->d[block + 1],
0,
second_bits - 1);
u32 v = low_part | (high_part << first_bits);
return v;
}
int main()
{
std::cout << "All tests passed.\n";
system("pause");
return 0;
}