Java Tutorial/Data Type/BigInteger

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

Содержание

and operation on BigInteger

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

}</source>





antNot operation on BigInteger

   <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.isProbablePrime

   <source lang="java">

import java.math.BigInteger; public class BigNumApp {

 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>





Calculate the power on a BigInteger

   <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>





Clear a bit in a BigInteger

   <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>





Convert BigInteger into another radix number

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] args) {
   BigInteger number = new BigInteger("2008");
   System.out.println("Number      = " + number);
   System.out.println("Binary      = " + number.toString(2));
   System.out.println("Octal       = " + number.toString(8));
   System.out.println("Hexadecimal = " + number.toString(16));
   number = new BigInteger("FF", 16);
   System.out.println("Number      = " + number);
   System.out.println("Number      = " + number.toString(16));
 }

}</source>





Create BigInteger from byte array

   <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>





Create BigInteger via long type variable

   <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>





Create BigInteger via string

   <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>





Divide one BigInteger from another BigInteger

   <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>





Do math operation for BigInteger

   <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>





Flip a bit in a BigInteger

   <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>





Get BigInteger from an object

   <source lang="java">

import java.math.BigDecimal; import java.math.BigInteger; /**

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**

* Operations on Object.
* 
* This class tries to handle null input gracefully.
* An exception will generally not be thrown for a null input.
* Each method documents its behaviour in more detail.
*
* @author 
* @since 1.0
* @version $Id: ObjectUtils.java 594336 2007-11-12 22:54:02Z bayard $
*/

public class Main {

 public static BigInteger getBigInteger(Object value) {
   BigInteger ret = null;
   if ( value != null ) {
       if ( value instanceof BigInteger ) {
           ret = (BigInteger) value;
       } else if ( value instanceof String ) {
           ret = new BigInteger( (String) value );
       } else if ( value instanceof BigDecimal ) {
           ret = ((BigDecimal) value).toBigInteger();
       } else if ( value instanceof Number ) {
           ret = BigInteger.valueOf( ((Number) value).longValue() );
       } else {
           throw new ClassCastException( "Not possible to coerce [" + value + "] from class " + value.getClass() + " into a BigInteger." );
       }
   }
   return ret;

} }</source>





Get byte array from BigInteger

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   BigInteger bi = new BigInteger("100100100111111110000", 2);
   byte[] bytes = bi.toByteArray();
 }

}</source>





Get the value of a bit

   <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>





Multiply one BigInteger to another BigInteger

   <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>





Negate a BigInteger

   <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>





not operation for BigInteger

   <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>





Operate with big integer values in code

   <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);
   bi1 = bi1.multiply(bi2);
   bi1 = bi1.subtract(bi2);
   bi1 = bi1.divide(bi2);
   bi1 = bi1.negate();
   int exponent = 2;
   bi1 = bi1.pow(exponent);
 }

}</source>





Operating with Big Integer Values

   <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>





or operation for BigInteger

   <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>





Parse and format to arbitrary radix <= Character.MAX_RADIX

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   int radix = 32;
   BigInteger bi = new BigInteger("vv", radix); 
   String s = bi.toString(radix); 
 }

}</source>





Parse and format to hexadecimal

   <source lang="java">

import java.math.BigInteger; public class Main {

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

}</source>





Parse binary string

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   BigInteger bi = new BigInteger("100101000111111110000", 2);
   byte[] bytes = bi.toByteArray();
 }

}</source>





Parse decimal string to create BigInteger

   <source lang="java">

import java.math.BigInteger; public class Main {

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

}</source>





Parse hexadecimal string to create BigInteger

   <source lang="java">

import java.math.BigInteger; public class Main {

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

}</source>





Parse octal string to create BigInteger

   <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>





Parsing and Formatting a Big Integer into Binary

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   BigInteger bi = new BigInteger("1023");
   // Parse and format to binary
   bi = new BigInteger("1111111111", 2); 
   String s = bi.toString(2); 
 }

}</source>





Parsing and Formatting a Big Integer into decimal

   <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>





Parsing and Formatting a Big Integer into octal

   <source lang="java">

import java.math.BigInteger; public class Main {

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

}</source>





Parsing and Formatting a Byte Array into Binary

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] bytes = new byte[] { (byte) 0x12, (byte) 0x0F, (byte) 0xF0 };
   // Create a BigInteger using the byte array
   BigInteger bi = new BigInteger(bytes);
   String s = bi.toString(2); 
   System.out.println(s);
 }

}</source>





Parsing and Formatting a Byte Array into decimal

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] bytes = new byte[] { (byte) 0x12, (byte) 0x0F, (byte) 0xF0 };
   BigInteger bi = new BigInteger(bytes);
   // Format to decimal
   String s = bi.toString(); 
 }

}</source>





Parsing and Formatting a Byte Array into Hexadecimal

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] bytes = new byte[] { (byte) 0x12, (byte) 0x0F, (byte) 0xF0 };
   BigInteger bi = new BigInteger(bytes);
   // Format to hexadecimal
   String s = bi.toString(16); 
   if (s.length() % 2 != 0) {
     s = "0" + s;
   }
 }

}</source>





Parsing and Formatting a Byte Array into Octal

   <source lang="java">

import java.math.BigInteger; public class Main {

 public static void main(String[] argv) throws Exception {
   byte[] bytes = new byte[] { (byte) 0x12, (byte) 0x0F, (byte) 0xF0 };
   BigInteger bi = new BigInteger(bytes);
   // Format to octal
   String s = bi.toString(8); 
   System.out.println(s);
 }

}</source>





Performing Bitwise Operations with BigInteger

   <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>





performing the modular exponential

   <source lang="java">

import java.math.BigInteger; import java.security.SecureRandom; public class MainClass {

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

} /*BitLength : 512 Number ^ Exponent MOD Modulus = Result Number 12429010321466148979282045876615924104266625036004424072013594464398465860901924701756511994455904802521087513832503046348373547015122338773846757329693089 Exponent 7960165959547365392190891717471267683026232484827038749061088628549083772881870434293423234456457412681666128278442829496431230428387587660778678814696147 Modulus 10339566307708626815760173877230355067432306461489808819560490502837798859869280782572410563219404567771359973589211641725154894564515306242543575863832237 Result 4664677700053277466905242082951352345941397508884099204138151141886794649976135281786429283508499942760627515243676288735812526509264162843510761720483915

  • /</source>





Retrieve the current bits in a byte array in twos-complement form.

   <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>





Set a bit for BigInteger

   <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>





Shift right in a BigInteger

   <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>





Shift the bits in a BigInteger

   <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>





Subtract one BigInteger from another BigInteger

   <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>





Use BigInteger

   <source lang="java">

import java.math.BigInteger; public class MainClass {

 public final static int pValue = 47;
 public final static int gValue = 71;
 public final static int XaValue = 9;
 public final static int XbValue = 14;
 public static void main(String[] args) throws Exception {
   BigInteger p = new BigInteger(Integer.toString(pValue));
   BigInteger g = new BigInteger(Integer.toString(gValue));
   System.out.println("p = " + p);
   System.out.println("g = " + g);
   BigInteger Xa = new BigInteger(Integer.toString(XaValue));
   BigInteger Xb = new BigInteger(Integer.toString(XbValue));
   System.out.println("Xa = " + Xa);
   System.out.println("Xb = " + Xb);
   BigInteger Ya = g.modPow(Xa, p);
   System.out.println("Ya = " + Ya);
   BigInteger Yb = g.modPow(Xb, p);
   System.out.println("Yb = " + Yb);
   BigInteger Ka = Ya.modPow(Xa, p);
   System.out.println("Users A, K = " + Ka);
   BigInteger Kb = Yb.modPow(Xb, p);
   System.out.println("Users B, K = " + Kb);
 }

}</source>





xor a BigInteger

   <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>