src/share/classes/java/math/BigInteger.java

Print this page




2902                 bytesCopied++;
2903             }
2904             byteArray[i] = (byte)nextInt;
2905         }
2906         return byteArray;
2907     }
2908 
2909     /**
2910      * Converts this BigInteger to an {@code int}.  This
2911      * conversion is analogous to a
2912      * <i>narrowing primitive conversion</i> from {@code long} to
2913      * {@code int} as defined in section 5.1.3 of
2914      * <cite>The Java&trade; Language Specification</cite>:
2915      * if this BigInteger is too big to fit in an
2916      * {@code int}, only the low-order 32 bits are returned.
2917      * Note that this conversion can lose information about the
2918      * overall magnitude of the BigInteger value as well as return a
2919      * result with the opposite sign.
2920      *
2921      * @return this BigInteger converted to an {@code int}.

2922      */
2923     public int intValue() {
2924         int result = 0;
2925         result = getInt(0);
2926         return result;
2927     }
2928 
2929     /**
2930      * Converts this BigInteger to a {@code long}.  This
2931      * conversion is analogous to a
2932      * <i>narrowing primitive conversion</i> from {@code long} to
2933      * {@code int} as defined in section 5.1.3 of
2934      * <cite>The Java&trade; Language Specification</cite>:
2935      * if this BigInteger is too big to fit in a
2936      * {@code long}, only the low-order 64 bits are returned.
2937      * Note that this conversion can lose information about the
2938      * overall magnitude of the BigInteger value as well as return a
2939      * result with the opposite sign.
2940      *
2941      * @return this BigInteger converted to a {@code long}.

2942      */
2943     public long longValue() {
2944         long result = 0;
2945 
2946         for (int i=1; i>=0; i--)
2947             result = (result << 32) + (getInt(i) & LONG_MASK);
2948         return result;
2949     }
2950 
2951     /**
2952      * Converts this BigInteger to a {@code float}.  This
2953      * conversion is similar to the
2954      * <i>narrowing primitive conversion</i> from {@code double} to
2955      * {@code float} as defined in section 5.1.3 of
2956      * <cite>The Java&trade; Language Specification</cite>:
2957      * if this BigInteger has too great a magnitude
2958      * to represent as a {@code float}, it will be converted to
2959      * {@link Float#NEGATIVE_INFINITY} or {@link
2960      * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
2961      * the return value is finite, this conversion can lose


3365     private byte[] magSerializedForm() {
3366         int len = mag.length;
3367 
3368         int bitLen = (len == 0 ? 0 : ((len - 1) << 5) + bitLengthForInt(mag[0]));
3369         int byteLen = (bitLen + 7) >>> 3;
3370         byte[] result = new byte[byteLen];
3371 
3372         for (int i = byteLen - 1, bytesCopied = 4, intIndex = len - 1, nextInt = 0;
3373              i>=0; i--) {
3374             if (bytesCopied == 4) {
3375                 nextInt = mag[intIndex--];
3376                 bytesCopied = 1;
3377             } else {
3378                 nextInt >>>= 8;
3379                 bytesCopied++;
3380             }
3381             result[i] = (byte)nextInt;
3382         }
3383         return result;
3384     }
















































































3385 }


2902                 bytesCopied++;
2903             }
2904             byteArray[i] = (byte)nextInt;
2905         }
2906         return byteArray;
2907     }
2908 
2909     /**
2910      * Converts this BigInteger to an {@code int}.  This
2911      * conversion is analogous to a
2912      * <i>narrowing primitive conversion</i> from {@code long} to
2913      * {@code int} as defined in section 5.1.3 of
2914      * <cite>The Java&trade; Language Specification</cite>:
2915      * if this BigInteger is too big to fit in an
2916      * {@code int}, only the low-order 32 bits are returned.
2917      * Note that this conversion can lose information about the
2918      * overall magnitude of the BigInteger value as well as return a
2919      * result with the opposite sign.
2920      *
2921      * @return this BigInteger converted to an {@code int}.
2922      * @see #intValueExact()
2923      */
2924     public int intValue() {
2925         int result = 0;
2926         result = getInt(0);
2927         return result;
2928     }
2929 
2930     /**
2931      * Converts this BigInteger to a {@code long}.  This
2932      * conversion is analogous to a
2933      * <i>narrowing primitive conversion</i> from {@code long} to
2934      * {@code int} as defined in section 5.1.3 of
2935      * <cite>The Java&trade; Language Specification</cite>:
2936      * if this BigInteger is too big to fit in a
2937      * {@code long}, only the low-order 64 bits are returned.
2938      * Note that this conversion can lose information about the
2939      * overall magnitude of the BigInteger value as well as return a
2940      * result with the opposite sign.
2941      *
2942      * @return this BigInteger converted to a {@code long}.
2943      * @see #longValueExact()
2944      */
2945     public long longValue() {
2946         long result = 0;
2947 
2948         for (int i=1; i>=0; i--)
2949             result = (result << 32) + (getInt(i) & LONG_MASK);
2950         return result;
2951     }
2952 
2953     /**
2954      * Converts this BigInteger to a {@code float}.  This
2955      * conversion is similar to the
2956      * <i>narrowing primitive conversion</i> from {@code double} to
2957      * {@code float} as defined in section 5.1.3 of
2958      * <cite>The Java&trade; Language Specification</cite>:
2959      * if this BigInteger has too great a magnitude
2960      * to represent as a {@code float}, it will be converted to
2961      * {@link Float#NEGATIVE_INFINITY} or {@link
2962      * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
2963      * the return value is finite, this conversion can lose


3367     private byte[] magSerializedForm() {
3368         int len = mag.length;
3369 
3370         int bitLen = (len == 0 ? 0 : ((len - 1) << 5) + bitLengthForInt(mag[0]));
3371         int byteLen = (bitLen + 7) >>> 3;
3372         byte[] result = new byte[byteLen];
3373 
3374         for (int i = byteLen - 1, bytesCopied = 4, intIndex = len - 1, nextInt = 0;
3375              i>=0; i--) {
3376             if (bytesCopied == 4) {
3377                 nextInt = mag[intIndex--];
3378                 bytesCopied = 1;
3379             } else {
3380                 nextInt >>>= 8;
3381                 bytesCopied++;
3382             }
3383             result[i] = (byte)nextInt;
3384         }
3385         return result;
3386     }
3387 
3388     /**
3389      * Converts this {@code BigInteger} to a {@code long}, checking
3390      * for lost information.  If the value of this {@code BigInteger}
3391      * is out of the range of the {@code long} type, then an
3392      * {@code ArithmeticException} is thrown.
3393      *
3394      * @return this {@code BigInteger} converted to a {@code long}.
3395      * @throws ArithmeticException if the value of {@code this} will
3396      * not exactly fit in a {@code long}.
3397      * @see BigInteger#longValue
3398      * @since  1.8
3399      */
3400     public long longValueExact() {
3401         if (mag.length <= 2 && bitLength() <= 63)
3402             return longValue();
3403         else
3404             throw new ArithmeticException("BigInteger out of long range");
3405     }
3406 
3407     /**
3408      * Converts this {@code BigInteger} to an {@code int}, checking
3409      * for lost information.  If the value of this {@code BigInteger}
3410      * is out of the range of the {@code int} type, then an
3411      * {@code ArithmeticException} is thrown.
3412      *
3413      * @return this {@code BigInteger} converted to an {@code int}.
3414      * @throws ArithmeticException if the value of {@code this} will
3415      * not exactly fit in a {@code int}.
3416      * @see BigInteger#intValue
3417      * @since  1.8
3418      */
3419     public int intValueExact() {
3420         if (mag.length <= 1 && bitLength() <= 31)
3421             return intValue();
3422         else
3423             throw new ArithmeticException("BigInteger out of int range");
3424     }
3425 
3426     /**
3427      * Converts this {@code BigInteger} to a {@code short}, checking
3428      * for lost information.  If the value of this {@code BigInteger}
3429      * is out of the range of the {@code short} type, then an
3430      * {@code ArithmeticException} is thrown.
3431      *
3432      * @return this {@code BigInteger} converted to a {@code short}.
3433      * @throws ArithmeticException if the value of {@code this} will
3434      * not exactly fit in a {@code short}.
3435      * @see BigInteger#shortValue
3436      * @since  1.8
3437      */
3438     public short shortValueExact() {
3439         if (mag.length <= 1 && bitLength() <= 31) {
3440             int value = intValue();
3441             if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE)
3442                 return shortValue();
3443         }
3444         throw new ArithmeticException("BigInteger out of short range");
3445     }
3446 
3447     /**
3448      * Converts this {@code BigInteger} to a {@code byte}, checking
3449      * for lost information.  If the value of this {@code BigInteger}
3450      * is out of the range of the {@code byte} type, then an
3451      * {@code ArithmeticException} is thrown.
3452      *
3453      * @return this {@code BigInteger} converted to a {@code byte}.
3454      * @throws ArithmeticException if the value of {@code this} will
3455      * not exactly fit in a {@code byte}.
3456      * @see BigInteger#byteValue
3457      * @since  1.8
3458      */
3459     public byte byteValueExact() {
3460         if (mag.length <= 1 && bitLength() <= 31) {
3461             int value = intValue();
3462             if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)
3463                 return byteValue();
3464         }
3465         throw new ArithmeticException("BigInteger out of byte range");
3466     }
3467 }