技术开发 频道

JAVA:3个常用的JDK对象详细解析

  大整型对象(BigInteger)

  BigInteger和BigDecimal相比,大部分的功能是相同的,主要的差异为BigInteger仅仅表示大整数的封装,而不能表示浮点数,与此同时,BigInteger还提供了大量的位操作运算,这个和C++中的BitSet非常类似。如果我们的在应用中只是需要大整数,那么应该首先该类,而不是BigDecimal,如果我们的应用逻辑中有多于64种状态(long表示64bits)的情况,可以考虑用BigInteger的位操作功能。

  ①基本的数学运算和数学函数应用:

 1     public static void main(String args[]) {
2 BigInteger bi1 = new BigInteger("1234567890123456890");
3 BigInteger bi2 = BigInteger.valueOf(123L);
4 System.out.printf("bi1 = %s after added\n",bi1.add(bi2));
5 System.out.printf("bi1 = %s after multiply\n",bi1.multiply(bi2));
6 System.out.printf("bi1 = %s after subtract\n",bi1.subtract(bi2));
7 System.out.printf("bi1 = %s after divide\n",bi1.divide(bi2));
8 System.out.printf("bi1 = %s after negate\n",bi1.negate());
9 int exponent = 2;
10 System.out.printf("bi1 = %s after pow\n",bi1.pow(exponent));
11 }
12 /* 输出结果如下:
13 bi1 = 1234567890123457013 after added
14 bi1 = 151851850485185197470 after multiply
15 bi1 = 1234567890123456767 after subtract
16 bi1 = 10037137318076885 after divide
17 bi1 = -1234567890123456890 after negate
18 bi1 = 1524157875323883924401765803688472100 after pow
19 */

  ②将BigInteger的值转换为各种进制的字符串表示:

 1     public static void main(String args[]) {
2 BigInteger number = new BigInteger("2008");
3 System.out.println("Number = " + number);
4 System.out.println("Binary = " + number.toString(2));
5 System.out.println("Octal = " + number.toString(8));
6 System.out.println("Hexadecimal = " + number.toString(16));
7 number = new BigInteger("FF", 16);
8 System.out.println("Number = " + number);
9 System.out.println("Number = " + number.toString(16));
10 }
11 /* 输出结果如下:
12 Number = 2008
13 Binary = 11111011000
14 Octal = 3730
15 Hexadecimal = 7d8
16 Number = 255
17 Number = ff
18 */

  ③ 从字符串构造各种进制的BigInteger对象:

 1     public static void main(String args[]) {
2 BigInteger bi = new BigInteger("120ff0", 16);
3 Integer ii = Integer.valueOf("120ff0", 16);
4 if (bi.intValue() == ii.intValue())
5 System.out.println("bi.intValue == ii.intValue");
6 System.out.println("int i = " + bi.intValue());
7
8 BigInteger bi2 = new BigInteger("100100100111111110000", 2);
9 System.out.println(bi2.intValue());
10 }
11 /* 输出结果如下:
12 bi.intValue == ii.intValue
13 int i = 1183728
14 1200112
15 */

  ④ 位操作:

1     public static void main(String args[]) {
2         byte[] bytes = new byte[] { 0x1, 0x00, 0x00 };
3         BigInteger bi = new BigInteger(bytes);
4         //相当于 & (~)的位运算。
5         System.out.println("andNot = " + bi.andNot(bi));
6         //相当于 | 的位运算
7         BigInteger bi2 = new BigInteger(bytes);
8         System.out.println("or = " + bi2.or(bi2));
9         //相当于 ~ 的位运算
10         BigInteger bi3 = new BigInteger(bytes);
11         System.out.println("not = " + bi3.not());
12         //相当于 & 的位运算
13         BigInteger bi4 = new BigInteger(bytes);
14         System.out.println("and = " + bi4.and(bi4));
15         //相当于 ^ 的位运算
16         BigInteger bi5 = new BigInteger(bytes);
17         System.out.println("xor = " + bi5.xor(bi5));
18     }
19     /*    输出结果如下:
20         andNot = 0
21         or = 65536
22         not = -65537
23         and = 65536
24         xor = 0
25     */
0
相关文章