Java by API/java.math/BigInteger

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

BigInteger: add(BigInteger val)

   <source lang="java">
  

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.");
 }

}


 </source>
   
  
 
  



BigInteger: andNot(BigInteger val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: bitLength()

   <source lang="java">
  

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.");
 }

}


 </source>
   
  
 
  



BigInteger: clearBit(int n)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: divide(BigInteger val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: doubleValue()

   <source lang="java">
  

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());
 }

}



 </source>
   
  
 
  



BigInteger: flipBit(int n)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: isProbablePrime(int certainty)

   <source lang="java">
  

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.");
 }

}


 </source>
   
  
 
  



BigInteger: modPow(BigInteger exponent, BigInteger m)

   <source lang="java">
  

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);
 }

}


 </source>
   
  
 
  



BigInteger: multiply(BigInteger val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: negate()

   <source lang="java">
 

import java.math.BigInteger; public class Main {

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

}


 </source>
   
  
 
  



BigInteger: not()

   <source lang="java">
 

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();
 }

}


 </source>
   
  
 
  



BigInteger: or(BigInteger val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: pow(int exponent)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: probablePrime(int bitLength, Random rnd)

   <source lang="java">
  

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));
   }

}



 </source>
   
  
 
  



BigInteger: setBit(int n)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: shiftLeft(int n)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: shiftRight(int n)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: subtract(BigInteger val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger.TEN

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: testBit(int n)

   <source lang="java">
 

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); 
 }

}


 </source>
   
  
 
  



BigInteger: toByteArray()

   <source lang="java">
 

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();
 }

}


 </source>
   
  
 
  



BigInteger: toString()

   <source lang="java">
 
 

import java.math.BigInteger; public class Main {

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

}


 </source>
   
  
 
  



BigInteger: toString(int radix)

   <source lang="java">
  

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.");
 }

}


 </source>
   
  
 
  



BigInteger: valueOf(long val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



BigInteger: xor(BigInteger val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



new BigInteger(byte[] val)

   <source lang="java">
 

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);
 }

}


 </source>
   
  
 
  



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

   <source lang="java">
  

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);
 }

}


 </source>
   
  
 
  



new BigInteger(String val)

   <source lang="java">
  

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());
 }

}



 </source>
   
  
 
  



new BigInteger(String val, int radix)

   <source lang="java">
 

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   BigInteger bi = new BigInteger("4407760", 8);
 }

}


 </source>