位置:java.math.BigInteger
作用:提供高精度整型数据类型及相关操作
一、基本介绍
BigInteger为不可变的任意精度的整数(对象创建后无法改变,每次运算均会产生一个新的对象)。
所有操作中,都以二进制补码形式表示 BigInteger(同Java 的基本整数类型)。
提供所有 Java 的基本整数操作符(* / + -等等)的对应物(一堆函数)。
提供 java.lang.Math 的所有相关方法,此外还提供模算术、GCD 计算(最大公约数)、质数测试、素数生成、位操作以及一些其他操作。
算术运算、逐位逻辑运算的语义完全模仿 Java 整数算术运算符的语义。
位移操作允许产生负位移距离,且忽略了无符号的右位移运算符(>>>)。
比较操作执行有符号的整数比较,类似于 Java 的关系运算符和相等性运算符执行的比较。
模算术操作用来计算余数、求幂和乘法可逆元。这些方法始终返回非负结果,范围在 0 和 (modulus - 1)(包括)之间。
位操作对其操作数的二进制补码表示形式的单个位进行操作。如有必要,操作数会通过扩展符号来包含指定的位。单一位操作不能产生与正在被操作的 BigInteger 符号不同的 BigInteger,因为它们仅仅影响单个位,并且此类提供的“无穷大词大小”抽象可保证在每个 BigInteger 前存在无穷多的“虚拟符号位”数。
当为任何输入参数传递 null 对象引用时,此类中的所有方法和构造方法都将抛出 NullPointerException。
二、字段
static BigInteger ONE //BigInteger常量1
static BigInteger TEN //BigInteger常量10
static BigInteger ZERO //BigInteger常量0
三、生成BigInteger对象
构造函数
BigInteger( byte[] val ) //将包含BigInteger的二进制补码表示形式的byte数组转换为BigInteger
BigInteger( int signum , byte[] magnitude ) //byte数组同上一条,signum表示符号:1为正,-1为负,0为零(signum为0时byte数组不可包含非零字节)
BigInteger( int bitLength , int certainty ,Random rnd ) //生成BigInteger伪随机数,它可能是(概率不小于1 - 1/2certainty)一个具有指定 bitLength 的素数
BigInteger( int numBits , Random rnd ) //生成BigInteger伪随机数,其值均匀分布于[ 0 , 2numBits - 1 )
BigInteger( String val ) //将 BigInteger 的十进制字符串表示形式转换为 BigInteger(可包含前导负号)
BigInteger( String val ,int radix ) //将指定基数(进制)的 BigInteger 的字符串表示形式转换为 BigInteger
四、常(suo)用(you)方法
算术运算(+ - * / % mod)
BigInteger add( BigInteger val )//返回其值为(this + val)的BigInteger
BigInteger subtract( BigInteger val )//返回其值为(this - val)的BigInteger
BigInteger negate()//返回其值是(-this)的BigInteger
BigInteger multiply( BigInteger val )//返回其值为(this * val)的BigInteger
BigInteger divide( BigInteger val )//返回其值为(this / val)的BigInteger
BigInteger remainder( BigInteger val )//返回其值为(this % val)的BigInteger
BigInteger[] divideAndRemainder( BigInteger val )//返回包含(this / val)后跟(this % val)的两个BigInteger的数组
BigInteger mod( BigInteger m )//返回其值为(this mod m)的BigInteger(与%不同,其值永远为正,下同)
BigInteger modInverse( BigInteger m ) //返回其值为(this-1mod m)的BigInteger
BigInteger modPow( BigInteger exponent , BigInteger m ) 返回其值为(thisexponentmod m)的BigInteger(exponent可以为负)
关系运算(==> >= < <= !=)
int compareTo( BigInteger val )//将此BigInteger与指定的BigInteger进行比较 ,this>val返回1,this==val返回0,this>val返回-1。使用:(a.compareTo(b) 0),表示关系运算符
boolean equals( Object x )//比较此BigInteger与指定的Object的相等性(当且仅当指定的 Object 是一个其值在数字上等于此 BigInteger 的 BigInteger 时,返回 true)
逻辑运算(& | ~ ^ &~)
BigInteger and( BigInteger val )//返回其值为(this & val)的BigInteger
BigInteger or( BigInteger val )//返回其值为(this | val)的BigInteger
BigInteger not()//返回其值为(~this)的BigInteger
BigInteger xor( BigInteger val )//返回其值为(this ^ val)的BigInteger
BigInteger andNot(BigInteger val)//返回其值为(this &~ val)的BigInteger
移位运算(<< >>)
BigInteger shiftLeft( int n ) //返回其值为(this << n)的BigInteger,n可为负,此时相当于执行右移操作
BigInteger shiftRight( int n )//返回其值为(this >> n)的BigInteger,执行符号扩展,n可为负,此时相当于执行左移操作
其他二进制运算
int bitCount()//返回此BigInteger的二进制补码表示形式中与符号不同的位的数量
int bitLength()//返回此BigInteger的最小的二进制补码表示形式的位数,不包括符号位,对于正 BigInteger,这等于常规二进制表示形式中的位数
boolean testBit( int n ) //当且仅当设置了指定的位时,返回true,即计算((this & (1 << n)) != 0)
BigInteger setBit( int n )//返回其值与设置了指定位的此BigInteger等效的BigInteger,即计算(this | (1 << n))
BigInteger clearBit( int n )//返回其值与清除了指定位的此BigInteger等效的BigInteger,即计算(this &~ (1 << n))
BigInteger flipBit( int n )//返回其值与对此BigInteger进行指定位翻转后的值等效的BigInteger,即计算(this ^ (1 << n))
int getLowestSetBit()//返回此BigInteger最右端(最低位)1比特的索引(即从此字节的右端开始到本字节中最右端1比特之间的0比特的位数)如果此 BigInteger 不包含一位,则返回 -1,即计算(this==0? -1 : log2(this & -this))
BigInteger与其他类型转换
static BigInteger valueOf( long val )//返回其值等于指定long型变量值的BigInteger(比long小的基本类型可以转化为long呦)
int intValue()//将此BigInteger转换为int,此转换类似于高精度变量向低精度变量的转换(如long到int),下同
long longValue()//将此BigInteger转换为long
float floatValue()//将此BigInteger转换为float
double doubleValue()//将此BigInteger转换为double
byte[] toByteArray()//返回一个byte数组,该数组包含此BigInteger的二进制补码表示形式
String toString()//返回此BigInteger的十进制字符串表示形式
String toString( int radix ) //返回此BigInteger的给定基数(进制)的字符串表示形式
数学函数
BigInteger abs()//返回其值是此BigInteger的绝对值的BigInteger
BigInteger gcd( BigInteger val ) //返回一个BigInteger,其值是abs(this)和abs(val)的最大公约数
BigInteger pow( int exponent ) //返回其值为(thisexponent)的BigInteger,exponent必须为正数
BigInteger max( BigInteger val ) //返回此BigInteger和val的最大值
BigInteger min( BigInteger val ) //返回此BigInteger和val的最小值
int hashCode() //返回此BigInteger的哈希码
boolean isProbablePrime( int certainty ) //如果此BigInteger可能为素数,则返回true,如果它一定为合数,则返回false。(certainty为调用方允许的不确定性的度量,如果该调用返回 true,则此 BigInteger 是素数的概率超出 (1 - 1/2certainty),此方法的执行时间与此参数的值是成比例的)
BigInteger nextProbablePrime() //返回大于此BigInteger的可能为素数的第一个整数
static BigInteger probablePrime( int bitLength , Random rnd ) //返回有可能是素数的、具有指定长度bitLength的正BigInteger
int signum() //返回此BigInteger的正负号,1为正,-1为负,0为零
一些写得不错的相关文章: