zoukankan      html  css  js  c++  java
  • BigInteger类(高精度整型)

    位置:java.math.BigInteger

    作用:提供高精度整型数据类型及相关操作

    一、基本介绍

    • BigInteger为不可变的任意精度的整数(对象创建后无法改变,每次运算均会产生一个新的对象)。
    • 所有操作中,都以二进制补码形式表示 BigInteger(同Java 的基本整数类型)。
    • 提供所有 Java 的基本整数操作符(* / + -等等)的对应物(一堆函数)。
    • 提供 java.lang.Math 的所有相关方法,此外还提供模算术、GCD 计算(最大公约数)、质数测试、素数生成、位操作以及一些其他操作。
    • 算术运算、逐位逻辑运算的语义完全模仿 Java 整数算术运算符的语义
    • 位移操作允许产生负位移距离,且忽略了无符号的右位移运算符(>>>)。
    • 比较操作执行有符号的整数比较,类似于 Java 的关系运算符和相等性运算符执行的比较。
    • 模算术操作用来计算余数、求幂和乘法可逆元。这些方法始终返回非负结果,范围在 0(modulus - 1)(包括)之间。
    • 位操作对其操作数的二进制补码表示形式的单个位进行操作。如有必要,操作数会通过扩展符号来包含指定的位。单一位操作不能产生与正在被操作的 BigInteger 符号不同的 BigInteger,因为它们仅仅影响单个位,并且此类提供的“无穷大词大小”抽象可保证在每个 BigInteger 前存在无穷多的“虚拟符号位”数。
    • 当为任何输入参数传递 null 对象引用时,此类中的所有方法和构造方法都将抛出 NullPointerException。  

    二、字段

    1. static BigInteger ONE  //BigInteger常量1
    2. static BigInteger TEN   //BigInteger常量10
    3. static BigInteger ZERO    //BigInteger常量0

    三、生成BigInteger对象

    构造函数

    1. BigInteger( byte[] val )   //将包含BigInteger的二进制补码表示形式的byte数组转换为BigInteger
    2. BigInteger( int signum , byte[] magnitude )  //byte数组同上一条,signum表示符号:1为正,-1为负,0为零(signum为0时byte数组不可包含非零字节)
    3. BigInteger( int bitLength , int certainty ,Random rnd )  //生成BigInteger伪随机数,它可能是(概率不小于1 - 1/2certainty)一个具有指定 bitLength 的素数
    4. BigInteger( int numBits , Random rnd )  //生成BigInteger伪随机数,其值均匀分布于[ 0 , 2numBits - 1 )
    5. BigInteger( String val )  //将 BigInteger 的十进制字符串表示形式转换为 BigInteger(可包含前导负号)
    6. BigInteger( String val ,int radix )  //将指定基数(进制)的 BigInteger 的字符串表示形式转换为 BigInteger

    四、常(suo)用(you)方法

    算术运算(+ - * / % mod)

    1. BigInteger add( BigInteger val )   //返回其值为(this + val)的BigInteger
    2. BigInteger subtract( BigInteger val )   //返回其值为(this - val)的BigInteger
    3. BigInteger negate()          //返回其值是(-this)的BigInteger
    4. BigInteger multiply( BigInteger val )  //返回其值为(this * val)的BigInteger
    5. BigInteger divide( BigInteger val )  //返回其值为(this / val)的BigInteger
    6. BigInteger remainder( BigInteger val )  //返回其值为(this % val)的BigInteger
    7. BigInteger[] divideAndRemainder( BigInteger val )  //返回包含(this / val)后跟(this % val)的两个BigInteger的数组
    8. BigInteger mod( BigInteger m )  //返回其值为(this mod m)的BigInteger(与%不同,其值永远为正,下同)
    9. BigInteger modInverse( BigInteger m )   //返回其值为(this-1 mod m)的BigInteger
    10. BigInteger modPow( BigInteger exponent , BigInteger m )   返回其值为(thisexponent mod m)的BigInteger(exponent可以为负)

    关系运算(==  >  >=  <  <=  !=)

    1. int  compareTo( BigInteger val )  //将此BigInteger与指定的BigInteger进行比较 ,this>val返回1,this==val返回0,this>val返回-1。使用:(a.compareTo(b) <op> 0),<op>表示关系运算符 
    2. boolean equals( Object x )  //比较此BigInteger与指定的Object的相等性(当且仅当指定的 Object 是一个其值在数字上等于此 BigInteger 的 BigInteger 时,返回 true)

    逻辑运算(&  |  ~  ^  &~)

    1. BigInteger and( BigInteger val )  //返回其值为(this & val)的BigInteger
    2. BigInteger or( BigInteger val )  //返回其值为(this | val)的BigInteger
    3. BigInteger not()          //返回其值为(~this)的BigInteger
    4. BigInteger xor( BigInteger val )  //返回其值为(this ^ val)的BigInteger
    5. BigInteger andNot(BigInteger val)  //返回其值为(this &~ val)的BigInteger

    移位运算(<<  >>)

    1. BigInteger shiftLeft( int n )  //返回其值为(this << n)的BigInteger,n可为负,此时相当于执行右移操作
    2. BigInteger shiftRight( int n )  //返回其值为(this >> n)的BigInteger,执行符号扩展,n可为负,此时相当于执行左移操作

    其他二进制运算

    1. int  bitCount()  //返回此BigInteger的二进制补码表示形式中与符号不同的位的数量
    2. int  bitLength()  //返回此BigInteger的最小的二进制补码表示形式的位数,不包括符号位,对于正 BigInteger,这等于常规二进制表示形式中的位数
    3. boolean testBit( int n )   //当且仅当设置了指定的位时,返回true,即计算((this & (1 << n)) != 0)
    4. BigInteger setBit( int n )   //返回其值与设置了指定位的此BigInteger等效的BigInteger,即计算(this | (1 << n))
    5. BigInteger clearBit( int n )  //返回其值与清除了指定位的此BigInteger等效的BigInteger,即计算(this &~ (1 << n))
    6. BigInteger flipBit( int n )  //返回其值与对此BigInteger进行指定位翻转后的值等效的BigInteger,即计算(this ^ (1 << n))
    7. int  getLowestSetBit()  //返回此BigInteger最右端(最低位)1比特的索引(即从此字节的右端开始到本字节中最右端1比特之间的0比特的位数)如果此 BigInteger 不包含一位,则返回 -1,即计算(this==0? -1 : log2(this & -this))

    BigInteger与其他类型转换

    1. static BigInteger valueOf( long val )  //返回其值等于指定long型变量值的BigInteger(比long小的基本类型可以转化为long呦)
    2. int intValue()  //将此BigInteger转换为int,此转换类似于高精度变量向低精度变量的转换(如long到int),下同
    3. long longValue()  //将此BigInteger转换为long
    4. float floatValue()  //将此BigInteger转换为float
    5. double doubleValue()  //将此BigInteger转换为double
    6. byte[] toByteArray()  //返回一个byte数组,该数组包含此BigInteger的二进制补码表示形式
    7. String toString()  //返回此BigInteger的十进制字符串表示形式
    8. String toString( int radix )  //返回此BigInteger的给定基数(进制)的字符串表示形式

     数学函数

    1. BigInteger abs()  //返回其值是此BigInteger的绝对值的BigInteger
    2. BigInteger gcd( BigInteger val )  //返回一个BigInteger,其值是abs(this)和abs(val)的最大公约数
    3. BigInteger pow( int exponent )  //返回其值为(thisexponent)的BigInteger,exponent必须为正数
    4. BigInteger max( BigInteger val )  //返回此BigInteger和val的最大值
    5. BigInteger min( BigInteger val )  //返回此BigInteger和val的最小值
    6. int  hashCode()  //返回此BigInteger的哈希码
    7. boolean isProbablePrime( int certainty )  //如果此BigInteger可能为素数,则返回true,如果它一定为合数,则返回false。(certainty为调用方允许的不确定性的度量,如果该调用返回 true,则此 BigInteger 是素数的概率超出 (1 - 1/2certainty),此方法的执行时间与此参数的值是成比例的)
    8. BigInteger nextProbablePrime()  //返回大于此BigInteger的可能为素数的第一个整数
    9. static BigInteger probablePrime( int bitLength , Random rnd )  //返回有可能是素数的、具有指定长度bitLength的正BigInteger
    10. int  signum()  //返回此BigInteger的正负号,1为正,-1为负,0为零

    JAVA API:https://docs.oracle.com/javase/7/docs/api/

    一些写得不错的相关文章:

    java.math.BigInteger系列教程

    Java中的大数处理类BigInteger和BigDecimar浅析

  • 相关阅读:
    软件测试 (三) 界面测试
    软件测试 (二) 六年软件测试感悟
    软件测试 (一) 软件测试方法大汇总
    第二阶段站立会议6
    第二阶段站立会议5
    构建之法阅读笔记04
    第二阶段站立会议4
    第二阶段站立会议3
    第二阶段站立会议2
    第二阶段站立会议1
  • 原文地址:https://www.cnblogs.com/Dumblidor/p/5405675.html
Copyright © 2011-2022 走看看