/** * Indicates if this number is a power of two (equals to 2<sup> * ({@link #bitLength bitLength()} - 1)</sup>). * * @return <code>true</code> if this number is a power of two; * <code>false</code> otherwise. */ public boolean isPowerOfTwo() { if (_size == 0) return false; final int n = _size - 1; for (int j = 0; j < n; j++) { if (_words[j] != 0) return false; } final int bitLength = MathLib.bitLength(_words[n]); return _words[n] == (1L << (bitLength - 1)); }
/** * Returns the number of bits in the minimal two's-complement representation * of the specified <code>long</code>, excluding a sign bit. * For positive <code>long</code>, this is equivalent to the number of bits * in the ordinary binary representation. For negative <code>long</code>, * it is equivalent to the number of bits of the positive value * <code>-(l + 1)</code>. * * @param l the <code>long</code> value for which the bit length is returned. * @return the bit length of <code>l</code>. */ public static int bitLength(long l) { int i = (int) (l >> 32); if (i > 0) return (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] + 32 : BIT_LENGTH[i >>> 8] + 40 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 48 : BIT_LENGTH[i >>> 24] + 56; if (i < 0) return bitLength(-++l); i = (int) l; return (i < 0) ? 32 : (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] : BIT_LENGTH[i >>> 8] + 8 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16 : BIT_LENGTH[i >>> 24] + 24; }
/** * Returns the number of bits in the minimal two's-complement representation * of the specified <code>long</code>, excluding a sign bit. * For positive <code>long</code>, this is equivalent to the number of bits * in the ordinary binary representation. For negative <code>long</code>, * it is equivalent to the number of bits of the positive value * <code>-(l + 1)</code>. * * @param l the <code>long</code> value for which the bit length is returned. * @return the bit length of <code>l</code>. */ public static int bitLength(long l) { int i = (int) (l >> 32); if (i > 0) return (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] + 32 : BIT_LENGTH[i >>> 8] + 40 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 48 : BIT_LENGTH[i >>> 24] + 56; if (i < 0) return bitLength(-++l); i = (int) l; return (i < 0) ? 32 : (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] : BIT_LENGTH[i >>> 8] + 8 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16 : BIT_LENGTH[i >>> 24] + 24; }
/** * Returns the number of bits in the minimal two's-complement representation * of the specified <code>long</code>, excluding a sign bit. * For positive <code>long</code>, this is equivalent to the number of bits * in the ordinary binary representation. For negative <code>long</code>, * it is equivalent to the number of bits of the positive value * <code>-(l + 1)</code>. * * @param l the <code>long</code> value for which the bit length is returned. * @return the bit length of <code>l</code>. */ public static int bitLength(long l) { int i = (int) (l >> 32); if (i > 0) return (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] + 32 : BIT_LENGTH[i >>> 8] + 40 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 48 : BIT_LENGTH[i >>> 24] + 56; if (i < 0) return bitLength(-++l); i = (int) l; return (i < 0) ? 32 : (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] : BIT_LENGTH[i >>> 8] + 8 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16 : BIT_LENGTH[i >>> 24] + 24; }
/** * Returns the number of bits in the minimal two's-complement representation * of the specified <code>long</code>, excluding a sign bit. * For positive <code>long</code>, this is equivalent to the number of bits * in the ordinary binary representation. For negative <code>long</code>, * it is equivalent to the number of bits of the positive value * <code>-(l + 1)</code>. * * @param l the <code>long</code> value for which the bit length is returned. * @return the bit length of <code>l</code>. */ public static int bitLength(long l) { int i = (int) (l >> 32); if (i > 0) return (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] + 32 : BIT_LENGTH[i >>> 8] + 40 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 48 : BIT_LENGTH[i >>> 24] + 56; if (i < 0) return bitLength(-++l); i = (int) l; return (i < 0) ? 32 : (i < 1 << 16) ? (i < 1 << 8) ? BIT_LENGTH[i] : BIT_LENGTH[i >>> 8] + 8 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16 : BIT_LENGTH[i >>> 24] + 24; }
/** * Returns the minimal number of bits to represent this large integer * in the minimal two's-complement (sign excluded). * * @return the length of this integer in bits (sign excluded). */ public int bitLength() { if (_size == 0) return 0; final int n = _size - 1; final int bitLength = MathLib.bitLength(_words[n]) + (n << 6) - n; return (this.isNegative() && this.isPowerOfTwo()) ? bitLength - 1 : bitLength; }
/** * Returns the value of this large integer as a <code>double</code>. * * @return the numeric value represented by this integer after conversion * to type <code>double</code>. */ public double doubleValue() { if (_size == 0) return 0; if (_size <= 1) return _isNegative ? -_words[0] : _words[0]; // Calculates bits length (ignores sign). final int n = _size - 1; final int bitLength = MathLib.bitLength(_words[n]) + (n << 6) - n; // Keep 63 most significant bits. int shift = 63 - bitLength; LargeInteger int63 = this.times2pow(shift); double d = MathLib.toDoublePow2(int63._words[0], -shift); return _isNegative ? -d : d; }
if (m < 0) return -toDoublePow2(-m, n); int bitLength = MathLib.bitLength(m); int shift = bitLength - 53; long exp = 1023L + 52 + n + shift; // Use long to avoid overflow.
if (m < 0) return -toDoublePow2(-m, n); int bitLength = MathLib.bitLength(m); int shift = bitLength - 53; long exp = 1023L + 52 + n + shift; // Use long to avoid overflow.
if (m < 0) return -toDoublePow2(-m, n); int bitLength = MathLib.bitLength(m); int shift = bitLength - 53; long exp = 1023L + 52 + n + shift; // Use long to avoid overflow.
if (m < 0) return -toDoublePow2(-m, n); int bitLength = MathLib.bitLength(m); int shift = bitLength - 53; long exp = 1023L + 52 + n + shift; // Use long to avoid overflow.