-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger.cpp
More file actions
134 lines (117 loc) · 2.11 KB
/
Integer.cpp
File metadata and controls
134 lines (117 loc) · 2.11 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
#include <cstring>
#include "Integer.h"
int highestbit(int n) {
int mask = 0x80000000, c = 32;
while ((c > 0) && !(mask & n)) {
n <<= 1;
c--;
}
return c;
}
Integer::Integer() {
m = NULL;
len = 0;
}
Integer::Integer(u32 n) {
m = new u32;
m[0] = n;
len = 1;
}
Integer::Integer(const Integer& n) {
m = new u32[n.len];
len = n.len;
memcpy(m, n.m, len*sizeof(u32));
}
const Integer& Integer::operator=(const Integer &n) {
delete [] m;
m = new u32[n.len];
len = n.len;
memcpy(m, n.m, len*sizeof(u32));
return *this;
}
Integer::~Integer() {
delete [] m;
}
String Integer::toHexString() {
char digits[17] = "0123456789abcdef";
u32 a, mask = 0x0000000F, *arr = (u32 *) this->m;
String s("0x");
int i, j = 32;
// eat up leading zeroes
while (!((arr[0] >> (j-4)) & mask))
j -= 4;
for (i = 0; i < this->len; i++) {
while (j > 0) {
a = (arr[i] >> (j-4)) & mask;
s = s + digits[a];
j -= 4;
}
j = 32;
}
return s;
}
Integer operator+(const Integer &n1, const Integer &n2) {
Integer sum;
int max = (n1.len > n2.len) ? n1.len : n2.len;
u32 *m = new u32[max];
u64 a, b, c = 0;
int i = max - 1;
while (i >= 0) {
a = (u64) n1.m[i];
b = (u64) n2.m[i];
c += a + b;
m[i] = c;
c >>= 32;
i--;
}
if (c) {
u32 *temp = new u32[max + 1];
memcpy((temp + 1), m, max*sizeof(u32));
temp[0] = c;
sum.m = temp;
sum.len = max + 1;
delete [] m;
}
else {
sum.m = m;
sum.len = max;
}
return sum;
}
Integer operator*(const Integer &n1, const Integer &n2) {
Integer prod;
u64 a, b, c = 0;
u32 m = n1.len, n = n2.len;
int i, j;
u64 *arr = new u64[(m+n)-1];
for (i = 0; i < (m+n)-1; i++)
arr[i] = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
a = n1.m[i];
b = n2.m[j];
arr[i+j] += a*b;
}
}
for (i = (m+n)-2; i >= 0; i--) {
arr[i] += c;
c = arr[i] >> 32;
}
if (c) {
prod.m = new u32[m+n];
prod.m[0] = (u32) c;
for (i = 1; i < (m+n); i++) {
prod.m[i] = (u32) arr[i-1];
}
prod.len = m+n;
}
else {
prod.m = new u32[(m+n)-1];
for (i = 0; i < (m+n)-1; i++) {
prod.m[i] = (u32) arr[i];
}
prod.len = (m+n)-1;
}
delete [] arr;
return prod;
}