52 Index leftmostBlockLen = 0;
53 while (leftmostBlock != 0) {
57 return leftmostBlockLen + (
len - 1) *
N;
64 block = newBit ? (block | mask) : (block & ~mask);
82 else if (
blk[i] > x.
blk[i])
114 #define DTRT_ALIASED(cond, op) \
116 BigUnsigned tmpThis; \
130 }
else if (b.
len == 0) {
136 bool carryIn, carryOut;
152 for (i = 0, carryIn =
false; i < b2->
len; i++) {
154 temp = a2->
blk[i] + b2->
blk[i];
157 carryOut = (temp < a2->
blk[i]);
161 carryOut |= (temp == 0);
168 for (; i < a2->
len && carryIn; i++) {
169 temp = a2->
blk[i] + 1;
170 carryIn = (temp == 0);
175 for (; i < a2->
len; i++)
192 throw "BigUnsigned::subtract: "
193 "Negative result in unsigned calculation";
195 bool borrowIn, borrowOut;
202 for (i = 0, borrowIn =
false; i < b.
len; i++) {
203 temp = a.
blk[i] - b.
blk[i];
206 borrowOut = (temp > a.
blk[i]);
209 borrowOut |= (temp == 0);
213 borrowIn = borrowOut;
217 for (; i < a.
len && borrowIn; i++) {
218 borrowIn = (a.
blk[i] == 0);
226 throw "BigUnsigned::subtract: Negative result in unsigned calculation";
229 for (; i < a.
len; i++)
297 return part1 | part2;
303 if (a.
len == 0 || b.
len == 0) {
318 bool carryIn, carryOut;
323 for (i = 0; i <
len; i++)
326 for (i = 0; i < a.
len; i++) {
328 for (i2 = 0; i2 <
N; i2++) {
329 if ((a.
blk[i] & (
Blk(1) << i2)) == 0)
344 for (j = 0, k = i, carryIn =
false; j <= b.
len; j++, k++) {
350 carryOut = (temp <
blk[k]);
353 carryOut |= (temp == 0);
360 for (; carryIn; k++) {
362 carryIn = (
blk[k] == 0);
367 if (
blk[len - 1] == 0)
389 throw "BigUnsigned::divideWithRemainder: Cannot write quotient and remainder into the same variable";
392 if (
this == &b || &q == &b) {
447 bool borrowIn, borrowOut;
470 q.
len = origLen - b.
len + 1;
473 for (i = 0; i < q.
len; i++)
494 for (j = 0, k = i, borrowIn =
false; j <= b.
len; j++, k++) {
496 borrowOut = (temp >
blk[k]);
498 borrowOut |= (temp == 0);
502 subtractBuf[k] = temp;
503 borrowIn = borrowOut;
507 for (; k < origLen && borrowIn; k++) {
508 borrowIn = (
blk[k] == 0);
509 subtractBuf[k] =
blk[k] - 1;
522 q.
blk[i] |= (
Blk(1) << i2);
525 blk[k] = subtractBuf[k];
531 if (q.
blk[q.
len - 1] == 0)
537 delete [] subtractBuf;
550 for (i = 0; i <
len; i++)
567 for (i = 0; i < b2->
len; i++)
569 for (; i < a2->
len; i++)
587 for (i = 0; i < b2->
len; i++)
589 for (; i < a2->
len; i++)
599 throw "BigUnsigned::bitShiftLeft: "
600 "Pathological shift amount not implemented";
606 Index shiftBlocks = b /
N;
607 unsigned int shiftBits = b %
N;
609 len = a.
len + shiftBlocks + 1;
612 for (i = 0; i < shiftBlocks; i++)
614 for (j = 0, i = shiftBlocks; j <= a.
len; j++, i++)
625 throw "BigUnsigned::bitShiftRight: "
626 "Pathological shift amount not implemented";
634 Index rightShiftBlocks = (b +
N - 1) /
N;
635 unsigned int leftShiftBits =
N * rightShiftBlocks - b;
638 if (rightShiftBlocks >= a.
len + 1) {
646 len = a.
len + 1 - rightShiftBlocks;
649 for (j = rightShiftBlocks, i = 0; j <= a.
len; j++, i++)
662 for (i = 0; i <
len && carry; i++) {
664 carry = (
blk[i] == 0);
682 throw "BigUnsigned::operator --(): Cannot decrement an unsigned zero";
685 for (i = 0; borrow; i++) {
686 borrow = (
blk[i] == 0);
void allocateAndCopy(Index c)
CmpRes compareTo(const BigUnsigned &x) const
friend Blk getShiftedBlock(const BigUnsigned &num, Index x, unsigned int y)
void bitOr(const BigUnsigned &a, const BigUnsigned &b)
void setBlock(Index i, Blk newBlock)
BigUnsigned::Blk getShiftedBlock(const BigUnsigned &num, BigUnsigned::Index x, unsigned int y)
void divideWithRemainder(const BigUnsigned &b, BigUnsigned &q)
#define DTRT_ALIASED(cond, op)
void bitShiftLeft(const BigUnsigned &a, int b)
unsigned int toUnsignedInt() const
void initFromPrimitive(X x)
void add(const BigUnsigned &a, const BigUnsigned &b)
void subtract(const BigUnsigned &a, const BigUnsigned &b)
unsigned long toUnsignedLong() const
NumberlikeArray< Blk >::Index Index
static const unsigned int N
void initFromSignedPrimitive(X x)
void setBit(Index bi, bool newBit)
void multiply(const BigUnsigned &a, const BigUnsigned &b)
unsigned short toUnsignedShort() const
void bitAnd(const BigUnsigned &a, const BigUnsigned &b)
void operator=(const BigUnsigned &x)
void bitXor(const BigUnsigned &a, const BigUnsigned &b)
Blk getBlock(Index i) const
void bitShiftRight(const BigUnsigned &a, int b)