JAVA 的BigInteger

本文深入解析了Java中的BigInteger类,详细介绍了其构造方法、常量、静态方法及各种数学运算、位操作、比较方法等,涵盖了BigInteger类的全面使用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.math.BigInteger;
import java.util.*;

public class Main {
	
	public static void main(String args[]) {
		Scanner cin = new Scanner(System.in);
		// 读到EOF
		while (cin.hasNext()) {
		}
		// 读入BigInteger
		BigInteger a = cin.nextBigInteger();

		// 构造方法
		// 将十进制字符串转化为BigInteger
		// public BigInteger(String val)
		BigInteger b = new BigInteger("3");
		// byte范围-128到+127 8为2进制数 c为767(1011111111)
		// public BigInteger(byte[] val)
		byte[] bt = new byte[] { 2, -1 };
		BigInteger c = new BigInteger(bt);
		// 将radix进制的字符串转化为BigInteger
		// public BigInteger(String val, int radix)
		BigInteger d = new BigInteger("3", 2);
		// 随机生成 0到2的numBits次方 -1的随机数
		// public BigInteger(int numBits, Random rnd)
		BigInteger e = new BigInteger(10, new Random());
		// signum为符号位 1为正 0为0 -1为负
		// public BigInteger(int signum, byte[] magnitude)
		BigInteger f = new BigInteger(-1, bt);
		// 随机生成一个 长度为bitLength的 可能性大于(1-1/(2的certainty次方))是素数 的数
		// public BigInteger(int bitLength, int certainty, Random rnd)
		BigInteger g = new BigInteger(10, 1, new Random());

		// 常量
		// 0
		// public static final BigInteger ZERO
		a = BigInteger.ZERO;
		// 1
		// public static final BigInteger ONE
		a = BigInteger.ONE;
		// 10
		// public static final BigInteger TEN
		a = BigInteger.TEN;

		// 静态方法
		// 随机生成一个 长度为bitLength的可能是素数的数
		// public static BigInteger probablePrime(int bitLength, Random rnd)
		BigInteger.probablePrime(10, new Random());
		// 值等于val的值
		// public static BigInteger valueOf(long val)
		BigInteger.valueOf(10);

		// 加法a+b
		// public BigInteger add(BigInteger val)
		a.add(b);
		// 减法a-b
		// public BigInteger subtract(BigInteger val)
		a.subtract(b);
		// 乘法a*b
		// public BigInteger subtract(BigInteger val)
		a.multiply(b);
		// 除法a/b
		// public BigInteger divide(BigInteger val)
		a.divide(b);
		// 取模a%b b需大于0 5mod3=2 -5mod3=1
		// public BigInteger mod(BigInteger m)
		a.mod(b);
		// 求余 5rem3=2 -5rem3=-2 5rem-3=2 -5rem-3=-2
		// public BigInteger remainder(BigInteger val)
		a.remainder(b);
		// [0]为a/b [1]为a%b
		// public BigInteger[] divideAndRemainder(BigInteger val)
		a.divideAndRemainder(b);

		// a==b?
		// public boolean equals(Object x)
		a.equals(b);
		// a的正负 正为1 0为0 负为-1
		// public int signum()
		a.signum();
		// 绝对值|a|
		// public BigInteger abs()
		a.abs();
		// 比较a>b返回1 a==b返回0 a<b返回-1
		// public BigInteger andNot(BigInteger val)
		a.compareTo(b);
		// 相反数-a
		// public BigInteger negate()
		a.negate();
		// max(a,b)
		// public BigInteger max(BigInteger val)
		a.max(b);
		// min(a,b)
		// public BigInteger min(BigInteger val)
		a.min(b);
		// 次方a的3次方
		// public BigInteger pow(int exponent)
		a.pow(3);
		// a的-1次方 %b
		// public BigInteger modInverse(BigInteger m)
		a.modInverse(b);
		// a的b次方 %c
		// public BigInteger modPow(BigInteger exponent,BigInteger m)
		a.modPow(b, c);

		// ~a
		// public BigInteger not()
		a.not();
		// a^b
		// public BigInteger xor(BigInteger val)
		a.xor(b);
		// a|b
		// public BigInteger or(BigInteger val)
		a.or(b);
		// a&b
		// public BigInteger divide(BigInteger val)
		a.and(b);
		// a左移n位 (a << n)
		// public BigInteger shiftLeft(int n)
		a.shiftLeft(10);
		// a右移n位 (a >> n)
		// public BigInteger shiftRight(int n)
		a.shiftRight(10);
		// a&(~b)
		// public BigInteger andNot(BigInteger val)
		a.andNot(b);
		// 二进制形式中把第n位二进制设为0 (a & ~(1<<n))
		// public BigInteger clearBit(int n)
		a.clearBit(10);
		// 二进制形式中把第n位二进制设为1 (a | (1<<n))
		// public BigInteger setBit(int n)
		a.setBit(10);
		// 二进制形式中第n位二进制是否为1 (a & (1<<n)) != 0)
		// public boolean testBit(int n)
		a.testBit(10);
		// 二进制形式中把第n位二进制翻转 (a ^ (1<<n))
		// public BigInteger flipBit(int n)
		a.flipBit(10);
		// 二进制形式中最低位1后面0的个数 (a == 0? -1 : log2(a & -a))
		// public int getLowestSetBit()
		a.getLowestSetBit();
		// 二进制形式中与符号不同的位的数量 7为3 -7为2
		// public int bitCount()
		a.bitCount();
		// 二进制形式中不包括符号位的长度
		// public int bitLength()
		a.bitLength();

		// a和b的最大公约数
		// public BigInteger gcd(BigInteger val)
		a.gcd(b);
		// a可能为素数返回true a一定为合数返回false 素数可能性大于(1-1/(2的certainty次方))
		// public boolean isProbablePrime(int certainty)
		a.isProbablePrime(10);
		// 大于a的可能为素数的第一个整数。
		// public BigInteger nextProbablePrime()
		a.nextProbablePrime();
		// a的哈希码
		// public int hashCode()
		a.hashCode();

		// a的二进制补码形式
		// public byte[] toByteArray()
		a.toByteArray();
		// a的十进制字符串形式
		// public String toString()
		a.toString();
		// a的radix进制字符串形式
		// public String toString(int radix)
		a.toString(2);
		// 将a转换为int
		// public int intValue()
		a.intValue();
		// 将a转换为long
		// public long longValue()
		a.longValue();
		// 将a转换为float
		// public float floatValue()
		a.floatValue();
		// 将a转换为double
		// public double doubleValue()
		a.doubleValue();

		// JAVA 1.8
		a.byteValueExact();
		a.intValueExact();
		a.longValueExact();
		a.shortValueExact();

		// 从类 java.lang.Number 继承的方法
		// 将a转换为short
		// public short shortValue()
		a.shortValue();
		// 将a转换为byte
		// public byte byteValue()
		a.byteValue();

		// 从类 java.lang.Object 继承的方法
		// public final Class<?> getClass()
		a.getClass();
		// public final void notify()
		a.notify();
		// public final void notifyAll()
		a.notifyAll();
		try {
			// public final void wait() throws InterruptedException
			a.wait();
			// public final void wait(long timeout) throws InterruptedException
			a.wait(10);
			// public final void wait(long timeout, int nanos) throws InterruptedException
			a.wait(10, 10);
		} catch (Exception exception) {
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值