栏目:Java8基础 作者:admin 日期:2014-12-28 评论:1 点击: 7,306 次
在java中,存在很多种类的数据类型,例如byte short char int float double long,而BigInteger属于其中一个比较特殊的数据类型,也是本教程关注的重点。BigInteger在JDK1.1中就已经存在了,属于java.math包的类。从名字来看,BigInteger比Integer表示数值的范围更大一些。BigInteger类的基本结构如下所示:
java.lang.Object
|_java.lang.Number
|_java.math.BigInteger
BigInteger已实现的接口:Serializable, Comparable<BigInteger>
先看一下JDK对BigInteger类的介绍:
Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types).
BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.
上面这段话的意思是:
BigInteger是不可变的任意精度的整数。所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
下面看看BigInteger有哪些重点的属性,主要的有下面三个:
(1)final int signum
signum属性是为了区分:正负数和0的标志位,JDK注释里面已经说的很明白了:
The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must have a signum of 0. This is necessary to ensures that there is exactly one representation for each BigInteger value.
(2)final int[] mag
mag是magnitude的缩写形式,mag数组是存储BigInteger数值大小的,采用big-endian的顺序,也就是高位字节存入低地址,低位字节存入高地址,依次排列的方式。JDK原文注释如下:
The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude. The magnitude must be "minimal" in that the most-significant int (mag[0]) must be non-zero. This is necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies that the BigInteger zero has a zero-length mag array.
(3)final static long LONG_MASK = 0xffffffffL;
This mask is used to obtain the value of an int as if it were unsigned。
BigInteger有四类构造函数,如下所示:
(1)通过字节数组生成一个BigInteger,有两种方法,如下所示:
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public BigInteger(byte[] val) { if (val.length == 0) { throw new NumberFormatException("Zero length BigInteger"); } if (val[0] < 0) { mag = makePositive(val); //此函数后面会讲解 signum = -1; } else { mag = stripLeadingZeroBytes(val); //此函数后面会讲解 signum = (mag.length == 0 ? 0 : 1); } } |
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public BigInteger(int signum, byte[] magnitude) { this.mag = stripLeadingZeroBytes(magnitude); if (signum < -1 || signum > 1) { throw (new NumberFormatException("Invalid signum value")); } if (this.mag.length == 0) { this.signum = 0; } else { if (signum == 0) { throw (new NumberFormatException("signum-magnitude mismatch")); } this.signum = signum; } } |
分析:上面的两个构造函数都是将包含BigInteger的二进制补码表示形式的byte数组转换为BigInteger。输入数组设定为 big-endian字节顺序:最高有效字节在第零个元素中。
(2)将 BigInteger 的十进制字符串表示形式转换为 BigInteger,如下所示:
1 2 3 4 |
public BigInteger(String val) { this(val, 10); } |
分析:将 BigInteger 的十进制字符串表示形式转换为 BigInteger。该字符串表示形式包括一个可选的减号,后跟一个或多个十进制数字序列。字符到数字的映射由Character.digit提供。该字符串不能包含任何其他字符(例如,空格)。
(3)构造一个随机生成的 BigInteger,它是在 0 到 (2numBits - 1)(包括)范围内均匀分布的值。
1 2 3 4 |
public BigInteger(int numBits, Random rnd) { this(1, randomBits(numBits, rnd)); } |
(4)构造一个随机生成的正BigInteger,它可能是一个具有指定 bitLength 的素数。
1 2 3 4 5 6 7 8 9 10 11 |
public BigInteger(int bitLength, int certainty, Random rnd) { BigInteger prime; if (bitLength < 2) { throw new ArithmeticException("bitLength < 2"); } prime = (bitLength < 95 ? smallPrime(bitLength, certainty, rnd) : largePrime(bitLength, certainty, rnd)); signum = 1; mag = prime.mag; } |
分析:prime指质数。
------====== 本站公告 ======------
金丝燕网,一个严谨的网站!