Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: BigInt.cpp
- Revision:
- 4:773aed3156c5
- Parent:
- 3:3fa3ceb0c69d
- Child:
- 5:beeb31f340a7
--- a/BigInt.cpp Fri Sep 20 15:55:19 2013 +0000
+++ b/BigInt.cpp Fri Oct 04 10:46:51 2013 +0000
@@ -75,7 +75,7 @@
delete[] bits;
bits = new uint32_t[l];
memset(bits, 0, sizeof(uint32_t)*l);
- for(int i = 0; i < length; ++i)
+ for(int i = length-1; i >=0; --i)
bits[i/4] |= data[i] << ((i%4)*8);
}
@@ -232,7 +232,33 @@
if(b == 1)
return (result = a);
-
+
+ result.size = a.size + b.size;
+ result.bits = new uint32_t[num(result.size)];
+ memset(result.bits, 0, sizeof(uint32_t)*num(result.size));
+ uint32_t carry = 0;
+ for(int i = 0; i < num(result.size); ++i)
+ {
+ uint32_t tmpA = 0, tmpB = 0;
+ if(i < num(a.size))
+ tmpA = a.bits[i];
+
+ if(i < num(b.size))
+ tmpB = b.bits[i];
+
+ uint64_t tmp = (uint64_t)tmpA * (uint64_t)tmpB + (uint64_t)carry;
+ result.bits[i] = tmp;
+ carry = tmp >> 32;
+ }
+
+ // trim result
+ uint8_t *tmp = (uint8_t*)result.bits;
+ uint32_t newSize = result.size;
+ while(tmp[newSize-1] == 0 && newSize > 0)
+ newSize--;
+ if(num(newSize) < num(result.size))
+ result.bits = (uint32_t*)realloc(result.bits, sizeof(uint32_t)*num(newSize));
+ result.size = newSize;
return result;
}