Java中BigInteger 的常见用法

目录

创建Biglnteger对象:

从基本类型创建:

从long值创建:

从int值创建(隐式转换为long):

从字符串创建:

从十进制字符串开始创建:

从二进制字符串创建(使用 2 作为基数):

从十六进制字符串创建(使用 16 作为基数):

算术运算:

加法

减法

乘法

除法

返回商

返回余数

同时返回商和余数

幂运算和取模

幂运算

取模

模幂运算

位运算

与运算

或运算

异或运算

左移

右移

比较

判断是否相等

比较大小

转换

转换为int

转换为long

转换为字符串

转换为二进制字符串

转换为十六进制字符串

其它常用方法

获取绝对值

获取相反数

判断是否为正数

判断是否为负数

判断是否为零

计算最大公约数

代码实例:


创建Biglnteger对象:

从基本类型创建:

从long值创建:

BigInteger bigInt = BigInteger.valueOf(123456789L);

从int值创建(隐式转换为long):

BigInteger bigInt = BigInteger.valueOf(12345);

解释:在 Java 中,BigInteger 类的 valueOf 方法接受一个 long 类型的参数,并返回一个表示该 long 值的 BigInteger 对象。当你从 int 值创建 BigInteger 时,实际上发生了隐式的类型转换,即 int 被自动转换为 long。当你使用 BigInteger.valueOf 方法并传入一个 int 值时,Java 会自动将这个 int 值转换为 long,然后再传递给 BigInteger.valueOf 方法。

从字符串创建:

从十进制字符串开始创建:

BigInteger bigInt = new BigInteger("123456789");

从二进制字符串创建(使用 2 作为基数):

BigInteger difference = bigInt1.subtract(bigInt2);
BigInteger bigInt = new BigInteger("101010", 2);

从十六进制字符串创建(使用 16 作为基数):

BigInteger bigInt = new BigInteger("ABCDEF", 16);

算术运算:

加法

BigInteger sum = bigInt1.add(bigInt2);

减法

BigInteger difference = bigInt1.subtract(bigInt2);

乘法

BigInteger product = bigInt1.multiply(bigInt2);

除法

返回商

BigInteger quotient = bigInt1.divide(bigInt2);

返回余数

BigInteger remainder = bigInt1.remainder(bigInt2);

同时返回商和余数

BigInteger[] result = bigInt1.divideAndRemainder(bigInt2);
BigInteger quotient = result[0];
BigInteger remainder = result[1];

幂运算和取模

幂运算

BigInteger power = bigInt.pow(exponent);

取模

BigInteger mod = bigInt.mod(modulus);

模幂运算

BigInteger modPowResult = bigInt.modPow(exponent, modulus);

位运算

与运算

BigInteger andResult = bigInt1.and(bigInt2);

或运算

BigInteger orResult = bigInt1.or(bigInt2);

异或运算

BigInteger xorResult = bigInt1.xor(bigInt2);

左移

BigInteger leftShift = bigInt.shiftLeft(n);

右移

BigInteger rightShift = bigInt.shiftRight(n);

比较

判断是否相等

boolean isEqual = bigInt1.equals(bigInt2);

比较大小

int comparison = bigInt1.compareTo(bigInt2); // 返回 -1, 0, 或 1

转换

转换为int

int intValue = bigInt.intValue();

转换为long

long longValue = bigInt.longValue();

转换为字符串

String stringValue = bigInt.toString();

转换为二进制字符串

String stringValue = bigInt.toString();

转换为十六进制字符串

String hexString = bigInt.toString(16);

其它常用方法

获取绝对值

BigInteger abs = bigInt.abs();

获取相反数

BigInteger negate = bigInt.negate();

判断是否为正数

boolean isPositive = bigInt.signum() > 0;

判断是否为负数

boolean isNegative = bigInt.signum() < 0;

判断是否为零

boolean isZero = bigInt.signum() == 0;

计算最大公约数

BigInteger gcd = bigInt1.gcd(bigInt2);

代码实例:

import java.math.BigInteger;

public class BigIntegerExample {
    public static void main(String[] args) {
        // 创建 BigInteger 对象
        BigInteger bigInt1 = new BigInteger("123456789");
        BigInteger bigInt2 = new BigInteger("987654321");

        // 算术运算
        BigInteger sum = bigInt1.add(bigInt2);
        BigInteger difference = bigInt1.subtract(bigInt2);
        BigInteger product = bigInt1.multiply(bigInt2);
        BigInteger quotient = bigInt1.divide(bigInt2);
        BigInteger remainder = bigInt1.remainder(bigInt2);

        // 幂运算和取模
        BigInteger power = bigInt1.pow(3);
        BigInteger mod = bigInt1.mod(bigInt2);
        BigInteger modPowResult = bigInt1.modPow(new BigInteger("2"), bigInt2);

        // 位运算
        BigInteger andResult = bigInt1.and(bigInt2);
        BigInteger orResult = bigInt1.or(bigInt2);
        BigInteger xorResult = bigInt1.xor(bigInt2);
        BigInteger leftShift = bigInt1.shiftLeft(2);
        BigInteger rightShift = bigInt1.shiftRight(2);

        // 比较
        boolean isEqual = bigInt1.equals(bigInt2);
        int comparison = bigInt1.compareTo(bigInt2);

        // 转换
        int intValue = bigInt1.intValue();
        long longValue = bigInt1.longValue();
        String stringValue = bigInt1.toString();
        String binaryString = bigInt1.toString(2);
        String hexString = bigInt1.toString(16);

        // 输出结果
        System.out.println("Sum: " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product: " + product);
        System.out.println("Quotient: " + quotient);
        System.out.println("Remainder: " + remainder);
        System.out.println("Power: " + power);
        System.out.println("Mod: " + mod);
        System.out.println("Mod Pow: " + modPowResult);
        System.out.println("AND: " + andResult);
        System.out.println("OR: " + orResult);
        System.out.println("XOR: " + xorResult);
        System.out.println("Left Shift: " + leftShift);
        System.out.println("Right Shift: " + rightShift);
        System.out.println("Is Equal: " + isEqual);
        System.out.println("Comparison: " + comparison);
        System.out.println("Int Value: " + intValue);
        System.out.println("Long Value: " + longValue);
        System.out.println("String Value: " + stringValue);
        System.out.println("Binary String: " + binaryString);
        System.out.println("Hex String: " + hexString);
    }
}
Sum: 1111111110
Difference: -864197532
Product: 121932631112635269
Quotient: 0
Remainder: 123456789
Power: 1881676371789154860897069
Mod: 123456789
Mod Pow: 478395063
AND: 39471121
OR: 1071639989
XOR: 1032168868
Left Shift: 493827156
Right Shift: 30864197
Is Equal: false
Comparison: -1
Int Value: 123456789
Long Value: 123456789
String Value: 123456789
Binary String: 111010110111100110100010101
Hex String: 75bcd15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值