Java零基础入门-大数
  CzYRWOUFyETw 2023年11月27日 29 0

前言

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

摘要

本文主要介绍了Java中BigInteger类的使用,包括创建、运算、比较等操作。同时,通过一个RSA加密算法的应用实例,展示了BigInteger类的实际应用场景。最后对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对象,bitLength参数表示BigInteger对象的二进制补码位数,rnd参数表示用于生成随机BigInteger对象的随机数生成器。这里不再赘述。

运算操作

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

BigInteger add(BigInteger val)    //加
BigInteger subtract(BigInteger val)   //减
BigInteger multiply(BigInteger val)   //乘
BigInteger divide(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的值为111111111000000000000

比较操作

BigInteger类的比较操作主要使用compareTo方法,该方法的返回值有三种情况:

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

例如:

BigInteger bi1 = new BigInteger("123456789");
BigInteger bi2 = new BigInteger("987654321");

System.out.println(bi1.compareTo(bi2));   //-1
System.out.println(bi2.compareTo(bi1));   //1
System.out.println(bi1.compareTo(new BigInteger("123456789")));  //0

其他操作

除了以上运算和比较操作,BigInteger类还提供了其他一些方法,如位运算、取反、与或非等操作。

BigInteger and(BigInteger val)  //与
BigInteger or(BigInteger val)   //或
BigInteger xor(BigInteger val)  //异或
BigInteger not()    //取反
BigInteger shiftLeft(int n)     //左移n位,相当于乘以2^n
BigInteger shiftRight(int n)    //右移n位,相当于除以2^n
int 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对象
BigInteger(long val)    //根据long类型的val创建BigInteger对象
BigInteger(int signum, byte[] magnitude)    //使用指定的大小和符号创建BigInteger对象

运算方法

BigInteger add(BigInteger val)    //加
BigInteger subtract(BigInteger val)   //减
BigInteger multiply(BigInteger val)   //乘
BigInteger divide(BigInteger val)     //除
BigInteger mod(BigInteger val)    //取模
BigInteger pow(int exponent)   //幂运算
int compareTo(BigInteger val)  //比较大小

比较方法

boolean equals(Object obj)     //判断当前BigInteger对象是否等于指定对象
int compareTo(BigInteger val)  //比较大小

其他方法

BigInteger and(BigInteger val)  //与
BigInteger or(BigInteger val)   //或
BigInteger xor(BigInteger val)  //异或
BigInteger not()    //取反
BigInteger shiftLeft(int n)     //左移n位,相当于乘以2^n
BigInteger shiftRight(int n)    //右移n位,相当于除以2^n
int bitCount()  //返回BigInteger对象表示的二进制补码中1的个数
String 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.divide(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 = 111111111000000000000
bi1 - bi2 = -86419753200000000000
bi1 * bi2 = 1219326311371232876715272110000000000
bi2 / bi1 = 8
bi2 % bi1 = 839506172000000000
bi1^3 = 18816763717869658727300850988427000000000000000000000000000000000000
bi1 > bi2? false
bi1 & bi2 = 9629778901000000000
bi1 | bi2 = 110337471810000000000
bi1 XOR bi2 = 110337471810000000000
~bi1 = -12345678900000000001
bi1 << 3 = 98765431280000000000000
bi2 >> 5 = 3086411031250000000
bi1 bit count = 41
bi2 bit count = 47
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月27日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   53   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   109   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
CzYRWOUFyETw