当前位置: 首页 > 图灵资讯 > 技术篇> Java零基础入门-大数

Java零基础入门-大数

来源:图灵教育
时间:2023-11-26 17:45:01

前言

在日常开发中,经常涉及到处理大数字的需求,如大整数操作、加密算法等。作为一种面向对象的编程语言,Java内置类库提供Biginteger和BigDecimal,可以方便地操作和处理大数字。本文将介绍Biginteger在Java中的应用,帮助零基础读者快速学习大数字处理。

摘要

本文主要介绍了Biginteger在Java中的应用,包括创建、操作、比较等操作。同时,Biginteger的实际应用场景通过RSA加密算法的应用实例展示。最后,分析了Biginteger的优缺点,并给出了测试用例和代码示例,帮助读者更好地理解和掌握Biginteger的使用。

简介

Biginteger类是Java内置用于处理大整数的类别。它可以表示任何大小的整数,而不受Java基本数据类型的范围限制。同时,Biginteger类还为实现加减乘除、取模、功率操作、比较大小等操作提供了丰富的方法。在实际开发中,Biginteger类常用于加密算法、数字签名等场景。

创建BigInteger对象的源代码分析

在Java中,可以使用new关键字创建Biginteger对象,其结构方法如下:

BigInteger(String val)

其中,val表示需要转换为大数字符串。例如:

BigInteger bi = new BigInteger("12345678901234567890");

上述代码表示,创建了一个BigInteger对象,其值为12345678901234567890。

此外,BigInteger对象的创建也可以使用以下方法:

BigInteger bi1 = BigInteger.valueOf(long val);BigInteger bi2 = new BigInteger(int bitLength, Random rnd);

其中,valueof方法可以将long类型的数字转换为biginteger对象,bitengeth参数表示biginteger对象的二进制补码位数,rnd参数表示生成随机biginteger对象的随机数生成器。这里就不赘述了。

运算操作

BigInteger类提供了丰富的操作方法,包括加法、减法、乘法和除法、模具提取、幂操作、比较尺寸等。这些方法的使用与普通值类型基本相同。

BigInteger add(BigInteger val)    //添加BigIntegerer subtract(BigInteger val)   ///减少BigInteger multiply(BigInteger val)   //乘坐BigInteger pide(BigInteger val)     //消除BigInteger mod(BigInteger val)    //取模BigInteger pow(int exponent)   //幂运算int compareTo(BigInteger val)  /比较大小

例如:

BigInteger bi1 = new BigInteger("12345678900000000000");BigInteger bi2 = new BigInteger("98765432100000000000");BigInteger bi3 = bi1.add(bi2);  //bi3值为111100000000
比较操作

compareto法主要用于BigInteger类的比较操作,该方法的返回值有三种:

  • 返回负整数:表示当前BigInteger对象小于传入的参数
  • 返回零:表示当前BigInteger对象等于输入的参数
  • 返回正整数:表示当前BigInteger对象大于传入的参数

例如:

BigInteger bi1 = new BigInteger("123456789");BigInteger bi2 = new BigInteger("987654321");System.out.println(bi1.compareTo(bi2));   //-1Systememestem//.out.println(bi2.compareTo(bi1));   //1System.out.println(bi1.compareTo(new BigInteger("123456789")));  //0
其他操作

除上述操作和比较操作外,BigInteger类还提供了位置操作、取反、和或非等其他操作方法。

BigInteger and(BigInteger val)  //与BigInteger or(BigInteger val)   //或BigIntegererer xor(BigInteger val)  /或者BigInteger not()    ///取反BigInteger shiftLeft(int n)     //左移n位,相当于乘以2^nBigInteger shiftRight(int n)    //右移n位相当于除以2^nint bitCount()  ///返回BigInteger对象表示的二进制补码中的1个数
应用场景案例

RSA加密算法是一种常用的加密算法,在实现中需要Biginteger类。具体实现方法可参考以下代码:

import java.math.BigInteger;import java.security.SecureRandom;public class RSA {        private final static BigInteger one = new BigInteger("1");    private final static SecureRandom random = new SecureRandom();    private BigInteger privateKey;    private BigInteger publicKey;    private BigInteger modulus;    public RSA(int bitLength) {        BigInteger p = BigInteger.probablePrime(bitLength / 2, random);        BigInteger q = BigInteger.probablePrime(bitLength / 2, random);        BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));        modulus = p.multiply(q);        publicKey = new BigInteger("65537");  ///公钥是固定值,通常为65537        privateKey = publicKey.modInverse(phi);    }    public byte[] encrypt(byte[] message) {        return (new BigInteger(message)).modPow(publicKey, modulus).toByteArray();    }    public byte[] decrypt(byte[] message) {        return (new BigInteger(message)).modPow(privateKey, modulus).toByteArray();    }}

在上述代码中,BigInteger类实现RSA算法中的加密和解密操作。具体实现原理可参考RSA算法的相关知识。

优缺点分析优点
  1. 整数可以表示在任何范围内,不受整形和长整形数据类型的范围限制。
  2. 大数字处理提供了丰富的操作和比较操作。
  3. 广泛应用于加密算法、数字签名等场景。
缺点
  1. 在处理大量数据时,BigInteger对象占用大量内存,因此在处理大量数据时可能会影响性能。
  2. 计算大数字运算需要很长时间,这可能会导致操作堵塞。
介绍代码方法的构造方法
BigInteger(String val)   ///根据字符串val创建BigInteger对象BigIntegerer(long val)    ///根据long类型的val创建BigInteger对象BigInter(int signum, byte[] magnitude)    ////用指定的大小和符号创建BigInteger对象
运算方法
BigInteger add(BigInteger val)    //添加BigIntegerer subtract(BigInteger val)   ///减少BigInteger multiply(BigInteger val)   //乘坐BigInteger pide(BigInteger val)     //除BigInteger mod(BigInteger val)    ///取模BigInteger pow(int exponent)   ///幂操作int compareTo(BigInteger val)  /比较大小
比较方法
boolean equals(Object obj)     ////判断当前BigInteger对象是否等于指定对象Integer compareTo(BigInteger val)  /比较大小
其他方法
BigInteger and(BigInteger val)  //与BigInteger or(BigInteger val)   /或者BigInteger xor(BigInteger val)  /或者BigInteger not()    ///取反BigInteger shiftLeft(int n)     //左移n位,相当于乘以2^nBigInteger shiftRight(int n)    //右移n位,相当于除以2^nint bitCount()  ///返回BigInteger对象表示的二进制补码中的String1 toString()   ////将BigInteger对象转换为十进制字符串byte[] toByteArray()   ///将BigInteger对象转换为二进制补码数组
测试用例测试代码
import java.math.BigInteger;public class Test {    public static void main(String[] args) {        BigInteger bi1 = new BigInteger("12345678900000000000");        BigInteger bi2 = new BigInteger("98765432100000000000");        System.out.println("bi1 + bi2 = " + bi1.add(bi2));  //bi1 + bi2 = 111111111000000000000        System.out.println("bi1 - bi2 = " + bi1.subtract(bi2)); //bi1 - bi2 = -86419753200000000000        System.out.println("bi1 * bi2 = " + bi1.multiply(bi2)); //bi1 * bi2 = 1219326311371232876715272110000000000        System.out.println("bi2 / bi1 = " + bi2.pide(bi1)); //bi2 / bi1 = 8        System.out.println("bi2 % bi1 = " + bi2.mod(bi1)); //bi2 % bi1 = 839506172000000000        System.out.println("bi1^3 = " + bi1.pow(3)); //bi1^3 = 18816763717869658727300850988427000000000000000000000000000000000000        System.out.println("bi1 > bi2? " + (bi1.compareTo(bi2) > 0)); //bi1 > bi2? false        System.out.println("bi1 & bi2 = " + bi1.and(bi2)); //bi1 & bi2 = 9629778901000000000        System.out.println("bi1 | bi2 = " + bi1.or(bi2)); //bi1 | bi2 = 110337471810000000000        System.out.println("bi1 XOR bi2 = " + bi1.xor(bi2)); //bi1 XOR bi2 = 110337471810000000000        System.out.println("~bi1 = " + bi1.not()); //~bi1 = -12345678900000000001        System.out.println("bi1 << 3 = " + bi1.shiftLeft(3)); //bi1 << 3 = 98765431280000000000000        System.out.println("bi2 >> 5 = " + bi2.shiftRight(5)); //bi2 >> 5 = 3086411031250000000        System.out.println("bi1 bit count = " + bi1.bitCount()); //bi1 bit count = 41        System.out.println("bi2 bit count = " + bi2.bitCount()); //bi2 bit count = 47    }}
测试结果
bi1 + bi2 = 111111111000000000000bi1 - bi2 = -86419753200000000000bi1 * bi2 = 1219326311371232876715272110000000000bi2 / bi1 = 8bi2 % bi1 = 839506172000000000bi1^3 = 18816763717869658727300850988427000000000000000000000000000000000000bi1 > bi2? falsebi1 & bi2 = 9629778901000000000bi1 | bi2 = 110337471810000000000bi1 XOR bi2 = 110337471810000000000~bi1 = -12345678900000000001bi1 << 3 = 98765431280000000000000bi2 >> 5 = 3086411031250000000bi1 bit count = 41bi2 bit count = 47