-// 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;
+template <class X>
+X BigInteger::convertToUnsignedPrimitive() const {
+ if (sign == negative)
+ throw "BigInteger::to<Primitive>: "
+ "Cannot convert a negative integer to an unsigned type";
+ else
+ return convertBigUnsignedToPrimitiveAccess<X>(mag);