-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhp_uint_as.cpp
67 lines (52 loc) · 1.14 KB
/
hp_uint_as.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
#include "hp_uint.h"
using namespace std;
namespace hpa
{
hp_uint& hp_uint::operator+=(const hp_uint& rhs)
{
int i;
ulint tmp, carry = 0;
for (i = 0; i < rhs.digits.size(); i++) {
if (i >= digits.size())
digits.push_back(0);
tmp = (ulint) (digits[i] + rhs.digits[i] + carry);
if (tmp > USINT_MAX)
carry = 1;
else
carry = 0;
digits[i] = (usint) tmp;
}
if (carry == 1)
if (i == digits.size())
digits.push_back(1);
else
digits[i] += 1;
return *this;
}
hp_uint& hp_uint::operator-=(const hp_uint& rhs)
{
int i;
ulint tmp, carry = 0;
for (i = 0; i < rhs.digits.size(); i++) {
tmp = (ulint) (digits[i] - carry - rhs.digits[i] + (USINT_MAX + 1));
if (tmp <= USINT_MAX)
carry = 1;
else
carry = 0;
digits[i] = (tmp);
}
if (carry == 1)
digits[i]--;
for (int k = digits.size() - 1; digits[k] == 0 && k > 0; k--)
digits.pop_back();
return *this;
}
const hp_uint hp_uint::operator+(const hp_uint& rhs) const
{
return hp_uint(*this) += rhs;
}
const hp_uint hp_uint::operator-(const hp_uint& rhs) const
{
return hp_uint(*this) -= rhs;
}
}