1 /* 2 * Modification History 3 * 4 * 2002-May-25 Jason Rohrer 5 * Created. 6 * Made getZero static. 7 */ 8 9 10 11 #ifndef BIG_INT_INCLUDED 12 #define BIG_INT_INCLUDED 13 14 15 16 /** 17 * A multi-byte integer representation. 18 * 19 * Some of the ideas used in this class were gleaned 20 * from studying Sun's Java 1.3 BigInteger implementation. 21 * 22 * @author Jason Rohrer. 23 */ 24 class BigInt { 25 26 27 28 public: 29 30 31 32 /** 33 * Constructs an integer. 34 * 35 * @param inSign the sign of this integer: 36 * -1 if negative, +1 if positive, and 0 if zero. 37 * @param inNumBytes the number of bytes in this integer. 38 * @param inBytes the bytes for this integer. 39 * Copied internally, so must be destroyed by caller. 40 */ 41 BigInt( int inSign, int inNumBytes, unsigned char *inBytes ); 42 43 44 45 /** 46 * Constructs an integer from a 32-bit int. 47 * 48 * @param inInt the int to use. 49 */ 50 BigInt( int inInt ); 51 52 53 54 ~BigInt(); 55 56 57 58 /** 59 * Adds an integer to this integer. 60 * 61 * @praram inOtherInt the int to add. 62 * Must be destroyed by caller. 63 * 64 * @return a newly allocated integer containing the sum. 65 * Must be destroyed by caller. 66 */ 67 BigInt *add( BigInt *inOtherInt ); 68 69 70 71 /** 72 * Subtracts an integer from this integer. 73 * 74 * @praram inOtherInt the int to subtract. 75 * Must be destroyed by caller. 76 * 77 * @return a newly allocated integer containing the difference. 78 * Must be destroyed by caller. 79 */ 80 BigInt *subtract( BigInt *inOtherInt ); 81 82 83 84 /** 85 * Gets whether this integer is less than another integer. 86 * 87 * @praram inOtherInt the integer test. 88 * Must be destroyed by caller. 89 * 90 * @return true if this integer is less than the other. 91 */ 92 char isLessThan( BigInt *inOtherInt ); 93 94 95 96 /** 97 * Gets whether this integer is equal to another integer. 98 * 99 * @praram inOtherInt the integer test. 100 * Must be destroyed by caller. 101 * 102 * @return true if this integer is equal to the other. 103 */ 104 char isEqualTo( BigInt *inOtherInt ); 105 106 107 108 /** 109 * Gets a copy of this integer. 110 * 111 * @return a newly allocated integer containing the copy. 112 * Must be destroyed by caller. 113 */ 114 BigInt *copy(); 115 116 117 118 /** 119 * Gets an integer equal to zero. 120 * 121 * @return a newly allocated integer containing zero. 122 * Must be destroyed by caller. 123 */ 124 static BigInt *getZero(); 125 126 127 128 /** 129 * Converts this integer to a decimal string. 130 * 131 * @return a \0-terminated ascii decimal string. 132 * Must be destroyed by caller. 133 */ 134 //char *convertToDecimalString(); 135 136 137 138 /** 139 * Converts this integer to a hex string. 140 * 141 * @return a \0-terminated ascii hexx string. 142 * Must be destroyed by caller. 143 */ 144 char *convertToHexString(); 145 146 147 148 /** 149 * Converts this integer to a 32-bit int. 150 * 151 * If this integer contains more than 32-bits, the high-order 152 * bits will be discarded, though the sign will be preserved. 153 */ 154 int convertToInt(); 155 156 157 158 /** 159 * -1 if negative, +1 if positive, and 0 if zero. 160 */ 161 int mSign; 162 163 int mNumBytes; 164 165 /** 166 * Integer is stored in big endian byte order. 167 */ 168 unsigned char *mBytes; 169 170 171 172 protected: 173 174 175 176 /** 177 * Flips the byte order of this integer. 178 * 179 * @return a newly allocated integer containing the flipped version. 180 * Must be destroyed by caller. 181 */ 182 BigInt *flipByteOrder(); 183 184 185 186 /** 187 * Computes the hex representation of a four-bit int. 188 * 189 * @param inInt the four-bit int to convert. 190 * 191 * @return the int as a hex ascii character, 192 * in {0, 1, ..., A, B, ..., F}. 193 */ 194 char fourBitIntToHex( int inInt ); 195 196 197 198 }; 199 200 201 202 #endif 203 204