File: BigInt.h

package info (click to toggle)
primrose 6%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,304 kB
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 92; perl: 67
file content (203 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download | duplicates (18)
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
/*
 * Modification History
 *
 * 2002-May-25    Jason Rohrer
 * Created.
 * Made getZero static.
 */



#ifndef BIG_INT_INCLUDED
#define BIG_INT_INCLUDED



/**
 * A multi-byte integer representation.
 *
 * Some of the ideas used in this class were gleaned
 * from studying Sun's Java 1.3 BigInteger implementation.
 *
 * @author Jason Rohrer.
 */
class BigInt {


        
    public:


        
        /**
         * Constructs an integer.
         *
         * @param inSign the sign of this integer:
         *   -1 if negative, +1 if positive, and 0 if zero.
         * @param inNumBytes the number of bytes in this integer.
         * @param inBytes the bytes for this integer.
         *   Copied internally, so must be destroyed by caller.
         */
        BigInt( int inSign, int inNumBytes, unsigned char *inBytes );


        
        /**
         * Constructs an integer from a 32-bit int.
         *
         * @param inInt the int to use.
         */
        BigInt( int inInt );



        ~BigInt();
        
        

        /**
         * Adds an integer to this integer.
         *
         * @praram inOtherInt the int to add.
         *   Must be destroyed by caller.
         *
         * @return a newly allocated integer containing the sum.
         *   Must be destroyed by caller.
         */
        BigInt *add( BigInt *inOtherInt );

        

        /**
         * Subtracts an integer from this integer.
         *
         * @praram inOtherInt the int to subtract.
         *   Must be destroyed by caller.
         *
         * @return a newly allocated integer containing the difference.
         *   Must be destroyed by caller.
         */
        BigInt *subtract( BigInt *inOtherInt );



        /**
         * Gets whether this integer is less than another integer.
         *
         * @praram inOtherInt the integer test.
         *   Must be destroyed by caller.
         *
         * @return true if this integer is less than the other.
         */        
        char isLessThan( BigInt *inOtherInt );



        /**
         * Gets whether this integer is equal to another integer.
         *
         * @praram inOtherInt the integer test.
         *   Must be destroyed by caller.
         *
         * @return true if this integer is equal to the other.
         */
        char isEqualTo( BigInt *inOtherInt );



        /**
         * Gets a copy of this integer.
         *
         * @return a newly allocated integer containing the copy.
         *   Must be destroyed by caller.
         */
        BigInt *copy();



        /**
         * Gets an integer equal to zero.
         *
         * @return a newly allocated integer containing zero.
         *   Must be destroyed by caller.
         */
        static BigInt *getZero();



        /**
         * Converts this integer to a decimal string.
         *
         * @return a \0-terminated ascii decimal string.
         *   Must be destroyed by caller.
         */
        //char *convertToDecimalString();

        

        /**
         * Converts this integer to a hex string.
         *
         * @return a \0-terminated ascii hexx string.
         *   Must be destroyed by caller.
         */
        char *convertToHexString();



        /**
         * Converts this integer to a 32-bit int.
         *
         * If this integer contains more than 32-bits, the high-order
         * bits will be discarded, though the sign will be preserved.
         */
        int convertToInt();
        
        
        
        /**
         * -1 if negative, +1 if positive, and 0 if zero.
         */
        int mSign;
        
        int mNumBytes;

        /**
         * Integer is stored in big endian byte order.
         */
        unsigned char *mBytes;


        
    protected:


        
        /**
         * Flips the byte order of this integer.
         *
         * @return a newly allocated integer containing the flipped version.
         *   Must be destroyed by caller.
         */ 
        BigInt *flipByteOrder();



        /**
         * Computes the hex representation of a four-bit int.
         *
         * @param inInt the four-bit int to convert.
         *
         * @return the int as a hex ascii character,
         *   in {0, 1, ..., A, B, ..., F}.
         */
        char fourBitIntToHex( int inInt );

        
        
    };



#endif