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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
/*
* Modification History
*
* 2002-May-25 Jason Rohrer
* Created.
* Changed to remove extra zeros when subtracting.
*/
#include "BigInt.h"
#include "minorGems/util/stringUtils.h"
#include <string.h>
#include <stdio.h>
BigInt::BigInt( int inSign, int inNumBytes, unsigned char *inBytes )
: mSign( inSign ), mNumBytes( inNumBytes ),
mBytes( new unsigned char[ inNumBytes ] ) {
memcpy( mBytes, inBytes, mNumBytes );
}
BigInt::BigInt( int inInt ) {
if( inInt > 0 ) {
mSign = 1;
}
else if( inInt == 0 ) {
mSign = 0;
}
else {
mSign = -1;
// flip sign so conversion works
inInt *= -1;
}
mNumBytes = 4;
int bytesToSkip = 0;
int baseFactor = 1;
mBytes = new unsigned char[ 4 ];
int i;
for( i=mNumBytes-1; i>=mNumBytes-4; i-- ) {
mBytes[i] = 0xFF & ( inInt / baseFactor );
baseFactor *= 256;
}
if( mBytes[0] == 0 ) {
bytesToSkip++;
if( mBytes[1] == 0 ) {
bytesToSkip++;
if( mBytes[2] == 0 ) {
bytesToSkip++;
if( mBytes[3] == 0 ) {
bytesToSkip++;
}
}
}
}
mNumBytes -= bytesToSkip;
unsigned char *temp = new unsigned char[ mNumBytes ];
memcpy( temp, &( mBytes[ bytesToSkip ] ), mNumBytes );
delete [] mBytes;
mBytes = temp;
}
BigInt::~BigInt() {
delete [] mBytes;
}
BigInt *BigInt::add( BigInt *inOtherInt ) {
//char *stringA = convertToHexString();
//char *stringB = inOtherInt->convertToHexString();
//printf( "Adding %s to %s\n", stringA, stringB );
//delete [] stringA;
//delete [] stringB;
if( mSign == 0 && inOtherInt->mSign == 0 ) {
return getZero();
}
else if( mSign == 0 && inOtherInt->mSign != 0 ) {
return inOtherInt->copy();
}
else if( mSign != 0 && inOtherInt->mSign == 0 ) {
return copy();
}
else if( mSign == -1 && inOtherInt->mSign == -1 ) {
// adding two negatives
// add them as positives, then flip sign of result
BigInt *copyA = copy();
BigInt *copyB = inOtherInt->copy();
copyA->mSign = 1;
copyB->mSign = 1;
BigInt *result = copyA->add( copyB );
delete copyA;
delete copyB;
result->mSign = -1;
return result;
}
else if( mSign == 1 && inOtherInt->mSign == -1 ) {
// flip other sign and subtract
BigInt *copy = inOtherInt->copy();
copy->mSign = 1;
BigInt *result = subtract( copy );
delete copy;
return result;
}
else if( mSign == -1 && inOtherInt->mSign == 1 ) {
// flip our sign and subtract
BigInt *copy = this->copy();
copy->mSign = 1;
BigInt *result = inOtherInt->subtract( copy );
delete copy;
return result;
}
else {
// both this int and arg int are positive
int maxLength = mNumBytes;
if( maxLength < inOtherInt->mNumBytes ) {
maxLength = inOtherInt->mNumBytes;
}
// leave room for carry
unsigned char *result = new unsigned char[ maxLength + 1 ];
// flip to little-endian order to make adding easier
BigInt *intA = flipByteOrder();
BigInt *intB = inOtherInt->flipByteOrder();
int sum = 0;
for( int i=0; i<maxLength; i++ ) {
int byteA = 0;
int byteB = 0;
if( i < intA->mNumBytes ) {
byteA = intA->mBytes[i];
}
if( i < intB->mNumBytes ) {
byteB = intB->mBytes[i];
}
// add in high-order bits from last sum
sum = byteA + byteB + ( sum >> 8 );
// truncate to low-order bits
result[i] = (unsigned char)sum;
}
// stick remaining carry into last byte
result[maxLength] = (unsigned char)( sum >> 8 );
BigInt *resultInt;
if( result[maxLength] == 0 ) {
// result array will be copied skipping carry byte
resultInt = new BigInt( 1, maxLength, result );
}
else {
// keep carry byte
resultInt = new BigInt( 1, maxLength + 1, result );
}
BigInt *flippedResult = resultInt->flipByteOrder();
delete intA;
delete intB;
delete resultInt;
delete [] result;
return flippedResult;
}
}
BigInt *BigInt::subtract( BigInt *inOtherInt ) {
//char *stringA = convertToHexString();
//char *stringB = inOtherInt->convertToHexString();
//printf( "Subtracting %s from %s\n", stringB, stringA );
//delete [] stringA;
//delete [] stringB;
if( mSign == 0 && inOtherInt->mSign == 0 ) {
return getZero();
}
else if( mSign != 0 && inOtherInt->mSign == 0 ) {
return copy();
}
else if( mSign == 0 && inOtherInt->mSign != 0 ) {
BigInt *copy = inOtherInt->copy();
// flip sign
copy->mSign = -( copy->mSign );
return copy;
}
else if( isEqualTo( inOtherInt ) ) {
return getZero();
}
else if( inOtherInt->mSign == -1 ) {
// flip sign and add
BigInt *copy = inOtherInt->copy();
copy->mSign = 1;
BigInt *result = add( copy );
delete copy;
return result;
}
else if( mSign == -1 ) {
// flip our sign and add
BigInt *copy = this->copy();
copy->mSign = 1;
BigInt *result = inOtherInt->add( copy );
delete copy;
// flip sign of result
result->mSign = -1;
return result;
}
else if( isLessThan( inOtherInt ) ) {
// trying to subtract a larger number
// flip subtraction order, then flip sign of result
BigInt *result = inOtherInt->subtract( this );
result->mSign = -( result->mSign );
return result;
}
else {
// we're subtracting a smaller positive number from
// a larger positive number
int maxLength = mNumBytes;
if( maxLength < inOtherInt->mNumBytes ) {
maxLength = inOtherInt->mNumBytes;
}
// result cannot be longer than the larger number
unsigned char *result = new unsigned char[ maxLength ];
// flip to little-endian order to make subtracting easier
BigInt *intA = this->flipByteOrder();
BigInt *intB = inOtherInt->flipByteOrder();
int borrow = 0;
for( int i=0; i<maxLength; i++ ) {
int byteA = 0;
int byteB = 0;
if( i < intA->mNumBytes ) {
byteA = intA->mBytes[i];
}
if( i < intB->mNumBytes ) {
byteB = intB->mBytes[i];
}
int diff = byteA - byteB - borrow;
// compute next borrow
borrow = 0;
while( diff < 0 ) {
borrow++;
diff += 256;
}
result[i] = (unsigned char)diff;
}
BigInt *resultInt;
if( borrow == 0 ) {
// skip any trailing 0 bytes
int bytesToSkip = 0;
int index = maxLength - 1;
while( index >= 0 && result[index] == 0 ) {
bytesToSkip ++;
index--;
}
// result array will be copied skipping borrow byte
// and skipping any trailing 0 bytes
resultInt = new BigInt( true, maxLength - bytesToSkip, result );
}
else {
printf( "Error: final borrow not zero: %d\n", borrow );
resultInt = getZero();
}
BigInt *flippedResult = resultInt->flipByteOrder();
delete intA;
delete intB;
delete resultInt;
delete [] result;
return flippedResult;
}
}
char BigInt::isLessThan( BigInt *inOtherInt ) {
if( mSign > inOtherInt->mSign ) {
return false;
}
else if( mSign < inOtherInt->mSign ) {
return true;
}
else {
// signs are equal
if( mSign == 0 ) {
return false;
}
else if( mSign == -1 && mNumBytes > inOtherInt->mNumBytes ) {
return true;
}
else if( mSign == 1 && mNumBytes > inOtherInt->mNumBytes ) {
return false;
}
else if( mSign == 1 && mNumBytes < inOtherInt->mNumBytes ) {
return true;
}
else if( mSign == -1 && mNumBytes < inOtherInt->mNumBytes ) {
return false;
}
else if( isEqualTo( inOtherInt ) ) {
return false;
}
else {
// numbers are the same length but are not equal
// start with high-order bytes
for( int i=0; i<mNumBytes; i++ ) {
if( mBytes[i] > inOtherInt->mBytes[i] ) {
if( mSign == 1 ) {
return false;
}
else {
return true;
}
}
else if ( mBytes[i] < inOtherInt->mBytes[i] ) {
if( mSign == 1 ) {
return true;
}
else {
return false;
}
}
}
// if we made it here, the numbers are equal...
// but we tested for equality above: must be an error
printf( "Error: equality test in isLessThan failed\n" );
return false;
}
}
}
char BigInt::isEqualTo( BigInt *inOtherInt ) {
if( mSign != inOtherInt->mSign ) {
return false;
}
else if( mNumBytes != inOtherInt->mNumBytes ) {
return false;
}
else {
// same length, same sign
for( int i=0; i<mNumBytes; i++ ) {
if( mBytes[i] != inOtherInt->mBytes[i] ) {
// mismatch found
return false;
}
}
// no mismatch found
return true;
}
}
BigInt *BigInt::copy() {
return new BigInt( mSign, mNumBytes, mBytes );
}
BigInt *BigInt::getZero() {
unsigned char bytes[0];
return new BigInt( 0, 0, bytes );
}
/*
THIS DOES NOT WORK!
char *BigInt::convertToDecimalString() {
char *tempString = new char[4];
char *resultString = new char[ mNumBytes * 3 + 1 ];
int stringIndex = 0;
for( int i=0; i<mNumBytes; i++ ) {
unsigned char currentByte = mBytes[i];
// pad with zeros
char *firstZero = "";
char *secondZero = "";
if( currentByte < 100 ) {
firstZero = "0";
if( currentByte < 10 ) {
secondZero = "0";
}
}
sprintf( tempString, "%s%s%d", firstZero, secondZero, currentByte );
memcpy( &( resultString[ stringIndex ] ), tempString, 3 );
stringIndex += 3;
}
resultString[ mNumBytes * 3 ] = '\0';
delete [] tempString;
return resultString;
}
*/
char *BigInt::convertToHexString() {
if( mSign == 0 ) {
return stringDuplicate( "0" );
}
else {
char *resultHexString = new char[ mNumBytes * 2 + 1 + 1 ];
int hexStringIndex = 0;
if( mSign == -1 ) {
resultHexString[0] = '-';
hexStringIndex++;
}
for( int i=0; i<mNumBytes; i++ ) {
unsigned char currentByte = mBytes[ i ];
int highBits = 0xF & ( currentByte >> 4 );
int lowBits = 0xF & ( currentByte );
resultHexString[ hexStringIndex ] = fourBitIntToHex( highBits );
hexStringIndex++;
resultHexString[ hexStringIndex ] = fourBitIntToHex( lowBits );
hexStringIndex++;
}
resultHexString[ hexStringIndex ] = '\0';
return resultHexString;
}
}
int BigInt::convertToInt() {
int result = 0;
int baseFactor = 1;
for( int i=mNumBytes-1; i>=mNumBytes-4; i-- ) {
if( i >=0 ) {
result += mBytes[i] * baseFactor;
}
baseFactor *= 256;
}
return mSign * result;
}
BigInt *BigInt::flipByteOrder() {
BigInt *returnInt = copy();
unsigned char *newBytes = returnInt->mBytes;
int newIndex = mNumBytes - 1;
for( int i=0; i<mNumBytes; i++ ) {
newBytes[ newIndex ] = mBytes[ i ];
newIndex--;
}
return returnInt;
}
char BigInt::fourBitIntToHex( int inInt ) {
char outChar[2];
if( inInt < 10 ) {
sprintf( outChar, "%d", inInt );
}
else {
switch( inInt ) {
case 10:
outChar[0] = 'A';
break;
case 11:
outChar[0] = 'B';
break;
case 12:
outChar[0] = 'C';
break;
case 13:
outChar[0] = 'D';
break;
case 14:
outChar[0] = 'E';
break;
case 15:
outChar[0] = 'F';
break;
default:
outChar[0] = '0';
break;
}
}
return outChar[0];
}
|