-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebi.h
99 lines (83 loc) · 2.67 KB
/
ebi.h
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
/**************************************************
Little-endian is implemented
ebi(0) has positive sign and 1 digit
int type is used for indexing, thus MAX_DATA_LENGTH should not larger than INT_MAX
I assume nobody will explicitly call the deconstructor
i.e. each instance has a valid data array
**************************************************/
#ifndef __EXTREMELYEASYTOUSEBIGINTEGER__
#define __EXTREMELYEASYTOUSEBIGINTEGER__
#include <iostream>
using std::istream;
using std::ostream;
#include <cstdint>
#ifndef UINT8_MAX
typedef unsigned char uint8_t;
#endif
#define EasyBigInteger ebi
#define MAX_DATA_LENGTH 10000
#define BYTE_SIZE 8
class ebi {
public:
//constructors and deconstructor
ebi();
ebi(int); //directly convert from an int
ebi(bool isPositive, unsigned int nXDigits, uint8_t* rawData);
ebi(const ebi&);
ebi(const char*);
~ebi();
//arithmetic operators
ebi operator+(const ebi&) const;
ebi operator-(const ebi&) const;
ebi operator*(const ebi&) const;
ebi operator/(const ebi&) const; //integer division: 3/2==1
ebi operator%(const ebi&) const; //by C99, a == (a/b*b) + a%b
ebi operator-() const;
ebi operator<<(unsigned int) const;
ebi operator>>(unsigned int) const;
//interface functions
unsigned int get_N_bytes() const;
uint8_t get_byte(unsigned int) const;
friend ostream& operator<<(ostream&, const ebi&);
friend istream& operator>>(istream&, ebi&);
//comparison operators
bool operator<(const ebi&) const;
bool operator>(const ebi&) const;
bool operator==(const ebi&) const;
bool operator!=(const ebi&) const;
bool operator<=(const ebi&) const;
bool operator>=(const ebi&) const;
//assignment operators
ebi& operator=(const ebi&);
ebi& operator+=(const ebi&);
ebi& operator-=(const ebi&);
ebi& operator*=(const ebi&);
ebi& operator/=(const ebi&);
//increment/decrement operators
ebi& operator++();
ebi& operator--();
ebi operator++(int);
ebi operator--(int);
//cast
explicit operator int() const;
private:
enum {negative, positive} sign;
unsigned int N_bytes;
uint8_t *data;
inline void base_initialization();
inline ebi base_addition(const ebi &a, const ebi &b) const;
inline ebi base_subtraction(const ebi& a, const ebi& b) const;
inline bool base_lessthan(const ebi& a, const ebi& b) const;
};
//math library
ebi abs(const ebi &);
ebi pow(ebi base, unsigned exponent);
ebi operator+(int n, const ebi &bn);
ebi operator-(int n, const ebi &bn);
ebi operator*(int n, const ebi &bn);
ebi operator/(int n, const ebi &bn);
ebi operator%(int n, const ebi &bn);
bool operator<(int n, const ebi &bn);
bool operator!=(int n, const ebi &bn);
bool operator==(int n, const ebi &bn);
#endif