-BigInteger::BigInteger(short x) {
- if (x > 0) {
- cap = 1;
- blk = new Blk[1];
- sign = positive;
- len = 1;
- blk[0] = Blk(x);
- } else if (x < 0) {
- cap = 1;
- blk = new Blk[1];
- sign = negative;
- len = 1;
- blk[0] = Blk(-x);
- } else
- sign = zero;
-}
-
-// CONVERTERS
-/*
- * The steps for conversion of a BigInteger to an
- * integral type are as follows:
- * 1. If the BigInteger is zero, return zero.
- * 2. If the BigInteger is positive:
- * 3. If it is more than one block long or its lowest
- * block has bits set out of the range of the target
- * type, throw an exception.
- * 4. Otherwise, convert the lowest block to the
- * target type and return it.
- * 5. If the BigInteger is negative:
- * 6. If the target type is unsigned, throw an exception.
- * 7. If it is more than one block long or its lowest
- * block has bits set out of the range of the target
- * type, throw an exception.
- * 8. Otherwise, convert the lowest block to the
- * target type, negate it, and return it.
- */
-
-namespace {
- // These masks are used to test whether a Blk has bits
- // set out of the range of a smaller integral type. Note
- // that this range is not considered to include the sign bit.
- const BigUnsigned::Blk lMask = ~0 >> 1;
- const BigUnsigned::Blk uiMask = (unsigned int)(~0);
- const BigUnsigned::Blk iMask = uiMask >> 1;
- const BigUnsigned::Blk usMask = (unsigned short)(~0);
- const BigUnsigned::Blk sMask = usMask >> 1;
-}
-
-BigInteger::operator unsigned long() const {
- switch (sign) {
- case zero:
- return 0;
- case positive:
- if (len == 1)
- return blk[0];
- else
- throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long";
- case negative:
- throw "BigInteger operator unsigned long() const: Cannot convert a negative integer to an unsigned type";
- default:
- throw "BigInteger: Internal error";
- }
-}
+BigInteger::BigInteger(long x) : sign(signOf(x)), mag(magOf<long , unsigned long >(x)) {}
+BigInteger::BigInteger(int x) : sign(signOf(x)), mag(magOf<int , unsigned int >(x)) {}
+BigInteger::BigInteger(short x) : sign(signOf(x)), mag(magOf<short, unsigned short>(x)) {}