Java by API/java.math/BigInteger

Материал из Java эксперт
Перейти к: навигация, поиск

BigInteger: add(BigInteger val)

   
import java.math.BigInteger;
public class Main {
  public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
      n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
  }
}





BigInteger: andNot(BigInteger val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.andNot(bi);
  }
}





BigInteger: bitLength()

   
import java.math.BigInteger;
public class Main {
  public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
      n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
  }
}





BigInteger: clearBit(int n)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.clearBit(3);
  }
}





BigInteger: divide(BigInteger val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");
    BigInteger bi2 = BigInteger.valueOf(123L);
    bi1 = bi1.divide(bi2);
  }
}





BigInteger: doubleValue()

   
import java.math.BigInteger;
/*
Here"s Long.MAX_VALUE: 9223372036854775807
Here"s a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24
 */
public class MainClass {
  public static void main(String[] args) {
    System.out.println("Here"s Long.MAX_VALUE: " + Long.MAX_VALUE);
    BigInteger bInt = new BigInteger("3419229223372036854775807");
    System.out.println("Here"s a bigger number: " + bInt);
    System.out.println("Here it is as a double: " + bInt.doubleValue());
  }
}





BigInteger: flipBit(int n)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.flipBit(3);
  }
}





BigInteger: isProbablePrime(int certainty)

   
import java.math.BigInteger;
public class Main {
  public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
      n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
  }
}





BigInteger: modPow(BigInteger exponent, BigInteger m)

   
import java.math.BigInteger;
import java.security.SecureRandom;
public class Main {
  public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);
    BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
    BigInteger n = BigInteger.probablePrime(bitLength, rnd);
    BigInteger result = n.modPow(exponent, mod);
    System.out.println("Number ^ Exponent MOD Modulus = Result");
    System.out.println("Number");
    System.out.println(n);
    System.out.println("Exponent");
    System.out.println(exponent);
    System.out.println("Modulus");
    System.out.println(mod);
    System.out.println("Result");
    System.out.println(result);
  }
}





BigInteger: multiply(BigInteger val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");
    BigInteger bi2 = BigInteger.valueOf(123L);
    bi1 = bi1.multiply(bi2);
  }
}





BigInteger: negate()

  
 
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");
    bi1 = bi1.negate();
  }
}





BigInteger: not()

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.not();
  }
}





BigInteger: or(BigInteger val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.or(bi);
  }
}





BigInteger: pow(int exponent)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");
    int exponent = 2;
    bi1 = bi1.pow(exponent);
  }
}





BigInteger: probablePrime(int bitLength, Random rnd)

   
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
/*
 */
public class MainClass {
  public static void main(String[] unused) {
    Random prng = new SecureRandom();  // self-seeding
    System.out.println(BigInteger.probablePrime(10, prng));
    }
 
}





BigInteger: setBit(int n)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.setBit(3);
  }
}





BigInteger: shiftLeft(int n)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.shiftLeft(3);
  }
}





BigInteger: shiftRight(int n)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.shiftRight(1);
  }
}





BigInteger: subtract(BigInteger val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");
    BigInteger bi2 = BigInteger.valueOf(123L);
    bi1 = bi1.subtract(bi2);
  }
}





BigInteger.TEN

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] args) {
    BigInteger numberA = new BigInteger("98765432123456789");
    BigInteger numberB = BigInteger.TEN;
    numberA = numberA.add(numberB);
    System.out.println("numberA = " + numberA);
    numberA = numberA.multiply(numberB);
    System.out.println("numberA = " + numberA);
    numberA = numberA.subtract(numberB);
    System.out.println("numberA = " + numberA);
    numberA = numberA.divide(numberB);
    System.out.println("numberA = " + numberA);
    numberA = numberA.mod(numberB);
    System.out.println("numberA = " + numberA);
    numberA = numberA.pow(2);
    System.out.println("numberA = " + numberA);
    numberA = numberA.negate();
    System.out.println("numberA = " + numberA);
  }
}





BigInteger: testBit(int n)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    boolean b = bi.testBit(3); 
    b = bi.testBit(16); 
  }
}





BigInteger: toByteArray()

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[]  bytes = new byte[] { 0x1, 0x00, 0x00 };
    BigInteger bi = new BigInteger(bytes);
    bytes = bi.toByteArray();
  }
}





BigInteger: toString()

  
  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi = new BigInteger("1023"); 
    String s = bi.toString(); 
  }
}





BigInteger: toString(int radix)

   
import java.math.BigInteger;
public class Main {
  public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
      n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
  }
}





BigInteger: valueOf(long val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create via a string
    BigInteger bi1 = new BigInteger("1234567890123456890");
    // Create via a long
    BigInteger bi2 = BigInteger.valueOf(123L);
    bi1 = bi1.add(bi2);
  }
}





BigInteger: xor(BigInteger val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = new byte[] { 0x1, 0x00, 0x00 }; 
    BigInteger bi = new BigInteger(bytes);
    bi = bi.xor(bi);
  }
}





new BigInteger(byte[] val)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    // A negative value
    byte[] bytes = new byte[] { (byte) 0xFF, 0x00, 0x00 }; // -65536
    // A positive value
    bytes = new byte[] { 0x1, 0x00, 0x00 }; // 65536
    BigInteger bi = new BigInteger(bytes);
  }
}





new BigInteger(int bitLength, int certainty, Random rnd)

   
import java.math.BigInteger;
import java.security.SecureRandom;
public class Main {
  public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);
    BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
    BigInteger n = BigInteger.probablePrime(bitLength, rnd);
    BigInteger result = n.modPow(exponent, mod);
    System.out.println("Number ^ Exponent MOD Modulus = Result");
    System.out.println("Number");
    System.out.println(n);
    System.out.println("Exponent");
    System.out.println(exponent);
    System.out.println("Modulus");
    System.out.println(mod);
    System.out.println("Result");
    System.out.println(result);
  }
}





new BigInteger(String val)

   
import java.math.BigInteger;
/*
Here"s Long.MAX_VALUE: 9223372036854775807
Here"s a bigger number: 3419229223372036854775807
Here it is as a double: 3.419229223372037E24
 */
public class MainClass {
  public static void main(String[] args) {
    System.out.println("Here"s Long.MAX_VALUE: " + Long.MAX_VALUE);
    BigInteger bInt = new BigInteger("3419229223372036854775807");
    System.out.println("Here"s a bigger number: " + bInt);
    System.out.println("Here it is as a double: " + bInt.doubleValue());
  }
}





new BigInteger(String val, int radix)

  
import java.math.BigInteger;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigInteger bi = new BigInteger("4407760", 8);
  }
}