-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInt.cpp
348 lines (319 loc) · 5.02 KB
/
Int.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
* Int类的实现
* @Author: Bao Wenjie
* @Email: [email protected]
* @Date: 2020/10/2
*/
#pragma once
#include "Num.h"
#include <string>
const unsigned int ONES = 0X7FFFFFFF; //01..31..1
const unsigned int FFFF = 0XFFFFFFFF; //111111111
const unsigned long long FLOAT_ONES = 0XFFFFFFFFFFFFFFFF;
const unsigned int E_BASE = 1023;
Int::Int()
{
// 整数
this->num_type = NumType::int_;
// 补码
this->code_type = CodeType::complement_;
this->num.num = 0;
}
Int::Int(int num)
{
// 整数
this->num_type = NumType::int_;
// 补码
this->code_type = CodeType::complement_;
this->num.num = num;
}
Int::Int(int num, CodeType type)
{
// 整数
this->num_type = NumType::int_;
// 补码
this->code_type = type;
this->num.num = num;
}
Int::~Int()
{
}
/**
* 整数: 转反码
*/
void Int::convert_to_inverse_code()
{
// 原码 -> 反码
if (this->code_type == CodeType::true_)
{
// 负数
if (this->num.s.sign != 0)
{
this->num.s.decimal ^= ONES;
}
}
// 补码 -> 反码
else if (this->code_type == CodeType::complement_)
{
// 负数
if (this->num.s.sign != 0)
{
this->num.s.decimal -= 1;
}
}
// 反码 -> 反码
else
{
return;
}
this->code_type = CodeType::inverse_;
return;
}
/**
* 整数: 转原码
*/
void Int::convert_to_true_code()
{
// 反码 -> 原码
if (this->code_type == CodeType::inverse_)
{
// 负数
if (this->num.s.sign != 0)
{
this->num.s.decimal ^= ONES;
}
}
// 补码 -> 原码
else if (this->code_type == CodeType::complement_)
{
// 负数
if (this->num.s.sign != 0)
{
this->convert_to_inverse_code();
this->convert_to_true_code();
}
}
// 原码 -> 原码
else
{
return;
}
this->code_type = CodeType::true_;
return;
}
/**
* 整数: 转补码
*/
void Int::convert_to_complement_code()
{
// 原码 -> 补码
if (this->code_type == CodeType::true_)
{
this->convert_to_inverse_code();
this->convert_to_complement_code();
}
// 反码 -> 补码
else if (this->code_type == CodeType::inverse_)
{
// 负数
if (this->num.s.sign != 0)
{
this->num.s.decimal += 1;
}
}
// 补码 -> 补码
else
{
return;
}
this->code_type = CodeType::complement_;
return;
}
/**
* 按照二进制打印数字
*/
void Int::print_code_binary()
{
// 符号位
this->num_to_binary(&this->num.num, 32, 31);
printf(" ");
// 数字位
this->num_to_binary(&this->num.num, 31, 0);
printf("\n");
}
/**
* 十进制打印数字
*/
void Int::print_num()
{
// 转补码
this->convert_to_complement_code();
printf("十进制为:%d\n", this->num.num);
}
/**
* 转浮点数
*/
Float* Int::convert_to_float()
{
this->convert_to_true_code();
u_float num;
// 符号位
num.s.sign = this->num.s.sign;
// 阶码10010101000001010001111010111000010100011110101110001
unsigned long long tmp = this->num.s.decimal;
unsigned long long i = 0;
while (tmp > 1)
{
tmp >>= 1;
i++;
}
tmp = this->num.s.decimal;
num.s.exponent = i + E_BASE;
// 小数
num.s.decimal = (tmp << (52 - i));
// new对象
Float* ans = new Float(num.num, CodeType::true_);
return ans;
}
/**
* 转整数
*/
Int* Int::convert_to_int()
{
return this;
}
/**
* 获取十进制数字的字符串
*/
string Int::get_string()
{
string ans = "";
this->convert_to_complement_code();
ans += std::to_string(this->num.num);
return ans;
}
/**
* 获取num
*/
long long Int::get_num()
{
long long ans = 0;
ans |= this->num.num;
return ans;
}
/**
* 获取符号位
*/
unsigned int Int::get_sign()
{
return this->num.s.sign;
}
/**
* 获取数字位
*/
unsigned long long Int::get_decimal()
{
return this->num.s.decimal;
}
/**
* 设置符号位[0,1]
*/
void Int::set_sign(unsigned int sign)
{
this->num.s.sign = sign;
}
/**
* 设置整数位
*/
void Int::set_num(unsigned int num)
{
this->num.s.decimal = num;
}
/**
* 设置CodeType
*/
void Int::set_codetype(CodeType type)
{
this->code_type = type;
}
/**
* + 重载
*/
Int Int::operator+(Int& num)
{
// 获取A的补码
this->convert_to_complement_code();
long long a = this->get_num();
// 获取B的补码
num.convert_to_complement_code();
long long b = num.get_num();
int s = 0;
int c = 0;
for (int i(0); i < 32; i++)
{
int a_ = (a >> i) & 1;
int b_ = (b >> i) & 1;
// 本位和
int c1 = (a_) ^ (b_) ^ s;
// 进位
s = (a_ & b_) | (a_ & s) | (b_ & s);
//
c |= (c1 << i);
}
Int ans = { c, CodeType::complement_ };
return ans;
}
/**
* - 重载
*/
Int Int::operator-(Int& num)
{
// 获取A的补码
this->convert_to_complement_code();
long long a = this->get_num();
// 对num原码符号位取反
Int tmp_num = num;
tmp_num.convert_to_true_code();
tmp_num.set_sign(~tmp_num.get_sign());
tmp_num.convert_to_complement_code();
return *this + tmp_num;
}
/**
* * 重载
*/
Int Int::operator*(Int& num)
{
this->convert_to_true_code();
unsigned int a = this->get_num();
num.convert_to_true_code();
unsigned int b = num.get_num();
Int B, C;
B.set_num(b), B.set_sign(0);
C.set_num(0), C.set_sign(0);
while (a > 0)
{
if (a & 1)
{
C = C + B;
}
B.set_num(B.get_num() << 1);
a >>= 1;
}
C.set_sign(this->get_sign() ^ num.get_sign());
C.set_codetype(CodeType::true_);
return C;
}
/**
* / 重载
*/
Int Int::operator/(Int& num)
{
}
/**
* << 重载
*/
ostream& operator<<(ostream& os, Int num)
{
os << num.get_string();
return os;
}