[转]java--BigInteger(2007-07-13 17:37)

 

转自http://hi.baidu.com/secretsh/blog/item/f70791de239a7b59ccbf1a4c.html

public class BigInteger extends Number

构造子
BigInteger
 public BigInteger(byte val[]) throws NumberFormatException
把一个(带符号)整数的二进制补码表示的字节数组翻译为 BigInteger 。假定输入数组是 big-endian 格式(即最有效字节在位置[0]) (最有效字节的最有效位是符号位)。数组必须至少包含一个字节,否则将抛出 NumberFormatException 。
BigInteger
 public BigInteger(int signum,
                     byte magnitude[]) throws NumberFormatException
把一个整数的 sign-magnitude 表示法翻译为 BigInteger 。符号表示为一个整数数值 (-1 表示负数、0 表示零、1 表示正数)。数量用 big-endian 结构表示(即最有效字节在位置[0])。一个无效的符号数值或一个 0 符号数值伴随一个非零数量将导致 NumberFormatException 。零长度大小的数组是允许的,并且将得到结果 0 (不管给定的符号数值是什么) 。
BigInteger
 public BigInteger(String val,
                     int radix) throws NumberFormatException
把一个字符串翻译为 BigInteger ,该字符串包含可选的负号,后面跟着一个或多个指定进制的数字序列 (字符到数字的映射是由 Character.digit 提供的)。一个外部字符 (包括空白符),或一个超出从 Character.MIN_RADIX(2) 到 Character.MAX_RADIX(36) 范围的基数( 包括边界),将导致 NumberFormatException 异常。
BigInteger
 public BigInteger(String val) throws NumberFormatException
把一个字符串翻译为 BigInteger ,该字符串包含可选的负号,后面跟着一个或多个十进制数字序列。字符到数字的映射是由 Character.digit 提供的。任何外部字符 (包括空白符) 将导致 NumberFormatException 异常。
BigInteger
 public BigInteger(int numBits,
                   Random rndSrc) throws IllegalArgumentException
返回一个随机数,均匀分布在 [0, 2**numBits - 1] 之间 (假设由 rndSrc 提供一个公平的随机源)。 注意该构造子总是返回一个非负的 BigInteger 。 如果 numBits <0,则抛出 illegalargumentexception 。
BigInteger
 public BigInteger(int bitLength,
                     int certainty,
                   Random rnd)
返回一个指定 bitLength (可能是一个素数)的随机选择的 BigInter 。参数 certainty 是一个对调用者愿意忍受的不确定性的度量:该数是素数的概率将超过 1 - 1/2**certainty 。执行时间正比于确定性参数的值。用给定的随机数发生器选择进行素数测试的候选数。如果 bitLength <2 ,则抛出 arithmeticexception。
方法

valueOf

 public static BigInteger valueOf(long val)
返回一个是指定值的 BigInteger 。 给该工厂提供首选的 (long) 构造子是因为它允许对频繁使用的 BigIntegers (如 0 和 1)的重用,这种操作不需要输出常数。

add

 public BigInteger add(BigInteger val) throws ArithmeticException
返回一个 BigInteger ,其值是 (this + val) 。

subtract

 public BigInteger subtract(BigInteger val)
返回一个 BigInteger ,其值是 (this - val) 。

multiply

 public BigInteger multiply(BigInteger val)
返回一个 BigInteger ,其值是 (this * val) 。

divide

 public BigInteger divide(BigInteger val) throws ArithmeticException
返回一个 BigInteger ,其值是 (this / val) 。 如果 val == 0 ,则抛出 ArithmeticException 。

remainder

 public BigInteger remainder(BigInteger val) throws ArithmeticException
返回一个 BigInteger ,其值是 (this % val) 。 如果 val == 0,则抛出 ArithmeticException 。

divideAndRemainder

 public BigInteger[] divideAndRemainder(BigInteger val) throws ArithmeticException
返回一个包含两个 BigIntegers 的数组。 返回值的第一个 ([0]) 元素是商 (this / val), 第二个 ([1]) 元素是余数 (this % val) 。如果 val == 0 ,则抛出 ArithmeticException 。

pow

 public BigInteger pow(int exponent) throws ArithmeticException
返回一个 BigInteger ,其值是 (this ** exponent) 。 如果 exponent <0(因为该操作将产生一个非整型值),则抛出 arithmeticexception 。注意:指数是一个整型数而不是 biginteger 。

gcd

 public BigInteger gcd(BigInteger val)
返回值为 abs(this) 和 abs(val) 最大公分母的 BigInteger 。 如果 this == 0 && val == 0,则返回 0。

abs

 public BigInteger abs()
返回一个 BigInteger ,它是该数值的绝对值。

negate

 public BigInteger negate()
返回一个 BigInteger ,其值是 (-1 * this ) 。

signum

 public int signum()
返回该数值的符号数 (根据该数的值是正、零或负返回 -1 、 0 或 1 ) 。

mod

 public BigInteger mod(BigInteger m)
返回一个 BigInteger ,其值是 this mod m 。 如果 m <= 0,则抛出 arithmeticexception。

modPow

 public BigInteger modPow(BigInteger exponent,
                          BigInteger m)
返回一个 BigInteger ,其值是 (this ** exponent) mod m 。 如果 exponent == 1, 返回值是 (this mod m) 。 如果 exponent <0 , 返回值是 (this ** -exponent)的模多重逆。如果 m <="0,则抛出" arithmeticexception 。

modInverse

 public BigInteger modInverse(BigInteger m) throws ArithmeticException
返回 this 取模 m 的模多重逆。 如果 m<= 0 或 this 没有多重逆 mod m (比如 gcd(this, m) !="1),则抛出" arithmeticexception 。

shiftLeft

 public BigInteger shiftLeft(int n)
返回一个 BigInteger ,其值是 (this << n)。计算 floor(this * 2**n)。

shiftRight

 public BigInteger shiftRight(int n)
返回一个 BigInteger ,其值是 (this >> n)。 执行符号扩展(计算 floor(this / 2**n))。

and

 public BigInteger and(BigInteger val)
返回一个 BigInteger ,其值是 (this & val) 。 (如果 this 和 val 二者都是负的,则该方法返回一个负数。)

or

 public BigInteger or(BigInteger val)
返回一个 BigInteger ,其值是 (this | val) 。 (如果 this 和 val 二者之一是负的,则该方法返回一个负数。)

xor

 public BigInteger xor(BigInteger val)
返回一个 BigInteger ,其值是 (this ^ val) 。 (如果 this 和 val 二者只有一个是负的,则该方法返回一个负数。)

not

 public BigInteger not()
返回一个 BigInteger ,其值是 (~this) 。 (如果 this 是非负的,则该方法返回一个负数。)

andNot

 public BigInteger andNot(BigInteger val)
返回一个 BigInteger ,其值是 (this & ~val) 。 该方法等价于 and(val.not()),它是进行掩模操作的便捷方法。 (如果 this 是负数并且 val 是正数,则该方法返回一个负数。)

testBit

 public boolean testBit(int n) throws ArithmeticException
如果设置了指定位则返回 true 。 (计算 ((this & (1 << n)) !="0))。如果 n < 0,则抛出 arithmeticexception。

setBit

 public BigInteger setBit(int n) throws ArithmeticException
返回一个 BigInteger ,其值等于该数被设置指定位后所得值(计算 (this | (1 << n)))。 如果 n < 0 ,则抛出 arithmeticexception 。

clearBit

 public BigInteger clearBit(int n) throws ArithmeticException
返回一个 BigInteger ,其值等于该数指定位清零后所得值(计算 (this & ~(1 << n)))。 如果 n < 0,则抛出 arithmeticexception 。

flipBit

 public BigInteger flipBit(int n) throws ArithmeticException
返回一个 BigInteger ,其值等于该数指定位取反后所得值(计算 (this ^ (1 << n)))。 如果 n < 0,则抛出 arithmeticexception 。

getLowestSetBit

 public int getLowestSetBit()
返回该数最右端 (lowest-order)是 1 的位的索引 (即就是距最右端 1 位间的 0 位的个数 ) 。 如果该数没有 1 位,则返回 -1 (计算 (this==0? -1 : log2(this & -this)))。

bitLength

 public int bitLength()
返回该数的最小二进制补码表示的位的个数, 即 *不包括* 符号位 (ceil(log2(this <0 ? -this : this + 1)))。对正数来说,这等价于普通二进制表示的位的个数。

bitCount

 public int bitCount()
返回该数的二进制补码表示中不包扩符号位在内的位的个数。 该方法在 BigIntegers 之上实现位向量风格的集合时很有用。

isProbablePrime

 public boolean isProbablePrime(int certainty)
如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2**certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。

compareTo

 public int compareTo(BigInteger val)
根据该数值是小于、等于、或大于 val 返回 -1、0 或 1 。该方法在六个逻辑比较运算符 (<, ="=,">, >=, !=, <= ) 的操作中作为首选方法。进行这些比较的建议方法是:(x.compareto(y) 0) ,其中 是六个比较符中的一个。

equals

 public boolean equals(Object x)
如果 x 是一个 BigInteger 并且等于该数则返回 true 。 提供该方法的目的是使 BigIntegers 可以作为散列码关键字使用。
覆盖:
Object 中的 equals

min

 public BigInteger min(BigInteger val)
返回 BigInteger ,其值是 this 和 val 中的较小者。 若值相同,则两者都可能被返回。

max

 public BigInteger max(BigInteger val)
返回 BigInteger ,其值是 this 和 val 中的较大者。 若值相同,则两者都可能被返回。

hashCode

 public int hashCode()
为该对象计算一个散列码。
覆盖:
Object 中的 hashCode

toString

 public String toString(int radix)
返回表示该数的字符串,基数为给定基数 。 如果基数超出了 Character.MIN_RADIX(2) 到 Character.MAX_RADIX(36)(包括两者在内) 的范围,它会缺省设定为 10 (类似于 Integer.toString 的情形)。使用由 Character.forDigit 提供的数字到字符的映射,并且如果适当的话,还可以前置一个负号。 该表示法同 (String, int) 构造子兼容。

toString

 public String toString()
返回表示该数的字符串,基数为 10 。 使用由 Character.forDigit 提供的数字到字符的映射,并且如果合适的话,还可以前置一个负号。 该表示法同 (String) 构造子兼容,并且允许用 Java 的 + 运算符做字符串连接操作。
覆盖:
Object 中的 toString

toByteArray

 public byte[] toByteArray()
返回该数值的二进制补码表示。 数组是 big-endian 格式(即最有效字节在位置 [0]) 。该数组包含了需要表示该数的最小的字节数 (ceil((this.bitLength() + 1)/8)) 。该表示法同(byte[])构造子兼容。

intValue

 public int intValue()
把该数转换为 int 值。 标准的限制原语转换同《Java 语言规范》所定义的一样。
覆盖:
Number 中的 intValue

longValue

 public long longValue()
把该数转换为 long 型。标准的限制原语转换同《Java 语言规范》定义的一样。
覆盖:
Number 中的 longValue

floatValue

 public float floatValue()
把该数转换为 float 型。该操作类似于《Java 语言规范》中定义的 double-to-float 限制原语转换:如果数值太大以致不能表示为一个浮点数时,它将被转换为合适的无穷大或负无穷大。
覆盖:
Number 中的 floatValue

doubleValue

 public double doubleValue()
把该数转换为 double 型。 该操作类似于《Java 语言规范》中定义的 double-to-float 的限制原语转换:如果数值太大以致不能表示为一个双精度数,它将被转换为适当的无穷大或负无穷大。
覆盖:
Number 中的 doubleValue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值