Java Tutorial/Data Type/BigInteger

Материал из Java эксперт
Версия от 15:26, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

and operation on BigInteger

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





antNot operation on BigInteger

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

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





Calculate the power on a BigInteger

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





Clear a bit in a BigInteger

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





Convert BigInteger into another radix number

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





Create BigInteger from byte array

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





Create BigInteger via long type variable

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





Create BigInteger via string

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





Divide one BigInteger from another BigInteger

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





Do math operation for BigInteger

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





Flip a bit in a BigInteger

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





Get BigInteger from an object

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 <code>Object</code>.
 * 
 * This class tries to handle <code>null</code> input gracefully.
 * An exception will generally not be thrown for a <code>null</code> 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;
}
}





Get byte array from BigInteger

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





Get the value of a bit

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





Multiply one BigInteger to another BigInteger

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





Negate a BigInteger

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





not operation for BigInteger

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





Operate with big integer values in code

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





Operating with Big Integer Values

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





or operation for BigInteger

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





Parse and format to arbitrary radix <= Character.MAX_RADIX

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





Parse and format to hexadecimal

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





Parse binary string

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





Parse decimal string to create BigInteger

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





Parse hexadecimal string to create BigInteger

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





Parse octal string to create BigInteger

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





Parsing and Formatting a Big Integer into Binary

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





Parsing and Formatting a Big Integer into decimal

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





Parsing and Formatting a Big Integer into octal

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





Parsing and Formatting a Byte Array into Binary

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





Parsing and Formatting a Byte Array into decimal

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





Parsing and Formatting a Byte Array into Hexadecimal

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





Parsing and Formatting a Byte Array into Octal

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





Performing Bitwise Operations with BigInteger

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





performing the modular exponential

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





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

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





Set a bit for BigInteger

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





Shift right in a BigInteger

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





Shift the bits in a BigInteger

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





Subtract one BigInteger from another BigInteger

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





Use BigInteger

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





xor a BigInteger

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