Old snapshot `bigint-2006.05.03'; see the ChangeLog file.
[bigint/bigint.git] / BigUnsigned.cc
index 83e725d..ecf1055 100644 (file)
@@ -1,6 +1,5 @@
 /*
 * Matt McCutchen's Big Integer Library
-* http://mysite.verizon.net/mccutchen/bigint/
 */
 
 #include "BigUnsigned.hh"
@@ -20,7 +19,7 @@
 * Since 2005.01.06, NumberlikeArray uses `NULL' rather
 * than a real array if one of zero length is needed.
 * These constructors implicitly call NumberlikeArray's
-* default constructor, which sets `blk2 = NULL, cap = len = 0'.
+* default constructor, which sets `blk = NULL, cap = len = 0'.
 * So if the input number is zero, they can just return.
 * See remarks in `NumberlikeArray.hh'.
 */
@@ -30,7 +29,7 @@ BigUnsigned::BigUnsigned(unsigned long x) {
                ; // NumberlikeArray already did all the work
        else {
                cap = 1;
-               blk2 = new Blk[1];
+               blk = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        }
@@ -41,7 +40,7 @@ BigUnsigned::BigUnsigned(long x) {
                ;
        else if (x > 0) {
                cap = 1;
-               blk2 = new Blk[1];
+               blk = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        } else
@@ -53,7 +52,7 @@ BigUnsigned::BigUnsigned(unsigned int x) {
                ;
        else {
                cap = 1;
-               blk2 = new Blk[1];
+               blk = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        }
@@ -64,7 +63,7 @@ BigUnsigned::BigUnsigned(int x) {
                ;
        else if (x > 0) {
                cap = 1;
-               blk2 = new Blk[1];
+               blk = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        } else
@@ -76,7 +75,7 @@ BigUnsigned::BigUnsigned(unsigned short x) {
                ;
        else {
                cap = 1;
-               blk2 = new Blk[1];
+               blk = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        }
@@ -87,7 +86,7 @@ BigUnsigned::BigUnsigned(short x) {
                ;
        else if (x > 0) {
                cap = 1;
-               blk2 = new Blk[1];
+               blk = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        } else
@@ -428,7 +427,7 @@ void BigUnsigned::multiply(const BigUnsigned &a, const BigUnsigned &b) {
        for (i = 0; i < a.len; i++) {
                // For each 1-bit of that block...
                for (i2 = 0; i2 < N; i2++) {
-                       if ((a.blk[i] & (1 << i2)) == 0)
+                       if ((a.blk[i] & (Blk(1) << i2)) == 0)
                                continue;
                        /*
                        * Add b to this, shifted left i blocks and i2 bits.
@@ -563,8 +562,9 @@ void BigUnsigned::divideWithRemainder(const BigUnsigned &b, BigUnsigned &q) {
        * amusing story of this section of code.
        */
        Index origLen = len; // Save real length.
+       // 2006.05.03: Copy the number and then change the length!
+       allocateAndCopy(len + 1); // Get the space.
        len++; // Increase the length.
-       allocateAndCopy(len); // Get the space.
        blk[origLen] = 0; // Zero the extra block.
        
        // work2 holds part of the result of a subtraction; see above.
@@ -623,7 +623,7 @@ void BigUnsigned::divideWithRemainder(const BigUnsigned &b, BigUnsigned &q) {
                        * the region of work2 we copy is just [i, k).
                        */
                        if (!borrowIn) {
-                               q.blk[i] |= (1 << i2);
+                               q.blk[i] |= (Blk(1) << i2);
                                while (k > i) {
                                        k--;
                                        blk[k] = work2[k];
@@ -752,8 +752,10 @@ void BigUnsigned::operator ++() {
        }
        if (carry) {
                // Matt fixed a bug 2004.12.24: next 2 lines used to say allocateAndCopy(len + 1)
+               // Matt fixed another bug 2006.04.24:
+               // old number only has len blocks, so copy before increasing length
+               allocateAndCopy(len + 1);
                len++;
-               allocateAndCopy(len);
                blk[i] = 1;
        }
 }