bitLength() public method

public bitLength ( ) : int
return int
Exemplo n.º 1
0
        private static int countDigits(BigInteger number)
        {
            double factor     = Math.Log(2) / Math.Log(10);
            int    digitCount = (int)(factor * number.bitLength() + 1);

            if (BigInteger.TEN.pow(digitCount - 1).compareTo(number) > 0)
            {
                return(digitCount - 1);
            }
            return(digitCount);
        }
Exemplo n.º 2
0
 public static object reduce(BigInteger val)
 {
     //return (val.bitLength() < 32) ? (object)val.intValue() : val;
     int bitLength = val.bitLength();
     return (bitLength < 32)
         ? (object)val.intValue()
         : (bitLength < 64)
             ? (object)val.longValue()
             : val;
 }
Exemplo n.º 3
0
 /// <summary>Calculate Log2 of a BigInteger object value.</summary>
 /// <remarks>Calculate Log2 of a BigInteger object value.</remarks>
 /// <param name="val">BigInteger object value which will calculate with log2</param>
 /// <returns>Result of calculating of val as double value</returns>
 public static double logBigInteger(BigInteger val)
 {
     double LOG2 = java.lang.Math.log(2.0);
     int blex = val.bitLength() - 1022;
     if (blex > 0)
     {
         val = val.shiftRight(blex);
     }
     double res = java.lang.Math.log(val.doubleValue());
     return blex > 0 ? res + blex * LOG2 : res;
 }
Exemplo n.º 4
0
        private static bool runMillerRabin(BigInteger number, SecureRandom random
			)
        {
            if (number.compareTo(BigInteger.valueOf(3)) <= 0)
            {
                return number.compareTo(BigInteger.ONE) != 0;
            }
            // Ensures that temp > 1 and temp < n.
            BigInteger temp = BigInteger.ZERO;
            do
            {
                temp = new BigInteger(number.bitLength() - 1, random);
            }
            while (temp.compareTo(BigInteger.ONE) <= 0);
            // Screen out n if our random number happens to share a factor with n.
            if (!number.gcd(temp).Equals(BigInteger.ONE))
            {
                return false;
            }
            // For debugging, prints out the integer to test with.
            //System.out.println("Testing with " + temp);
            BigInteger d = number.subtract(BigInteger.ONE);
            // Figure s and d Values
            int s = 0;
            while ((d.mod(TWO)).Equals(BigInteger.ZERO))
            {
                d = d.divide(TWO);
                s++;
            }
            BigInteger curValue = temp.modPow(d, number);
            // If this works out, it's a prime
            if (curValue.Equals(BigInteger.ONE))
            {
                return true;
            }
            // Otherwise, we will check to see if this value successively
            // squared ever yields -1.
            for (int r = 0; r < s; r++)
            {
                // We need to really check n-1 which is equivalent to -1.
                if (curValue.Equals(number.subtract(BigInteger.ONE)))
                {
                    return true;
                }
                else
                {
                    // Square this previous number - here I am just doubling the
                    // exponent. A more efficient implementation would store the
                    // value of the exponentiation and square it mod n.
                    curValue = curValue.modPow(TWO, number);
                }
            }
            // If none of our tests pass, we return false. The number is
            // definitively composite if we ever get here.
            return false;
        }