關(guān)于Java大整數(shù)運(yùn)算之BigInteger類
一、BigInteger類簡(jiǎn)單介紹
我們都知道Integer的存儲(chǔ)范圍是-2^31~2^31-1(-2147483648~2147483647),當(dāng)我們要存儲(chǔ)比Integer更大的數(shù)字時(shí),java中就為我們提供了一個(gè)BigInteger類,方便我們?nèi)ヌ幚砀蟮臄?shù)。
BigInteger 類支持任意精度的整數(shù),也就是說(shuō)在運(yùn)算中 BigInteger 類可以準(zhǔn)確地表示任何大小的整數(shù)值。首先除了基本的操作加、減、乘、除,在該類中還封裝了其他很有用的操作,接下來(lái)將一一介紹。
二、BigInteger構(gòu)造方式
(1)構(gòu)造方式
很顯然,BigInteger是一個(gè)封裝類,就跟String類是一樣的。使用時(shí)需要導(dǎo)入import java.math.BigInteger;使用 BigInteger 類,首先要?jiǎng)?chuàng)建一個(gè) BigInteger 對(duì)象。BigInteger是一個(gè)有參構(gòu)造,需要傳入一個(gè)參數(shù),最常見(jiàn)的就是給定一個(gè)字符串,使用構(gòu)造方法public BigInteger(String val)構(gòu)造一個(gè)十進(jìn)制的BigInteger對(duì)象。
小貼士:該構(gòu)造方法可以發(fā)生NumberFormatException異常,當(dāng)字符串參數(shù)val中如果含有非數(shù)字字符就會(huì)發(fā)生該異常。
import java.math.BigInteger;
/*
* BigInteger演示
*/
public class Test {
public static void main(String[] args) {
BigInteger bigInteger=new BigInteger("1123");
}
}(2)輸入方式
普通輸入:
Scanner sc=new Scanner(System.in); BigInteger a=sc.BigIntegerNext();
循環(huán)輸入 :
while (sc.hasNextBigInteger()) { // 注意 while 處理多個(gè) case
}除了定義實(shí)例,我們也可以像使用String類那樣使用BigInteger類,給大家一個(gè)題目感受一下,該題就是本文開(kāi)頭所提及的三角形求解。
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
/*
* 三角形
*/
public class Triangle {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while (sc.hasNextBigInteger()) { // 注意 while 處理多個(gè) case
BigInteger []triangle=new BigInteger[3];
for(int i=0;i<3;i++) {
triangle[i]=sc.nextBigInteger();
}
Arrays.sort(triangle);
if(triangle[0].add(triangle[1]).compareTo(triangle[2])>0) {
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}
}三、BigInteger常見(jiàn)的成員方法
(1)方法介紹
| 方法名 | 含義 |
| public BigInteger add(BigInteger val) | 返回當(dāng)前大整數(shù)對(duì)象與參數(shù)指定的大整數(shù)對(duì)象的和。 |
| public BigInteger subtract(BigInteger val) | 返回當(dāng)前大整數(shù)對(duì)象與參數(shù)指定的大整數(shù)對(duì)象的差。 |
| public BigInteger multiply(BigInteger val) | 返回當(dāng)前大整數(shù)對(duì)象與參數(shù)指定的大整數(shù)對(duì)象的積。 |
| public BigInteger divide(BigInteger val) | 返回當(dāng)前大整數(shù)對(duì)象與參數(shù)指定的大整數(shù)對(duì)象的商。 |
| public BigInteger remainder(BigInteger val) | 返回當(dāng)前大整數(shù)對(duì)象與參數(shù)指定的大整數(shù)對(duì)象的余。 |
| public int compareTo(BigInteger val) | 返回當(dāng)前大整數(shù)對(duì)象與參數(shù)指定的大整數(shù)的比較結(jié)果, 返回值是1、-1或0,分別表示當(dāng)前 大整數(shù)對(duì)象大于、小于或等于參數(shù)指定的大整數(shù)。 |
| public BigInteger abs() | 返回當(dāng)前大整數(shù)對(duì)象的絕對(duì)值。 |
| public BigInteger pow(int a) | 返回當(dāng)前大整數(shù)對(duì)象的a次冪。 |
| public String toString() | 返回當(dāng)前大整數(shù)對(duì)象十進(jìn)制的字符串表示。 |
| public String toString(int p) | 返回當(dāng)前大數(shù)對(duì)象p進(jìn)制的字符串表示。 |
(2)方法使用演示
1.加減乘除余
import java.math.BigInteger;
import java.util.Scanner;
/*
* BigInteger演示
*/
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger a=sc.nextBigInteger();
BigInteger b=sc.nextBigInteger();
System.out.println(a.add(b));//加
System.out.println(a.subtract(b));//減
System.out.println(a.multiply(b));//乘
System.out.println(a.divide(b));//除
System.out.println(a.remainder(b));//余
}
}看結(jié)果對(duì)于超大數(shù)字,也是完美計(jì)算結(jié)果的。

2.比較
import java.math.BigInteger;
import java.util.Scanner;
/*
* BigInteger演示
*/
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger a=sc.nextBigInteger();
BigInteger b=sc.nextBigInteger();
System.out.println(a.compareTo(b));
}
}a>b返回結(jié)果為1。

3.絕對(duì)值和冪
import java.math.BigInteger;
import java.util.Scanner;
/*
* BigInteger演示
*/
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger a=sc.nextBigInteger();
BigInteger b=sc.nextBigInteger();
System.out.println(a.abs());
System.out.println(b.pow(100));
}
}
4.轉(zhuǎn)換成對(duì)應(yīng)進(jìn)制字符串
import java.math.BigInteger;
import java.util.Scanner;
/*
* BigInteger演示
*/
public class Test {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
BigInteger a=sc.nextBigInteger();
BigInteger b=sc.nextBigInteger();
String strTen=a.toString();
String strTwo=b.toString(2);
System.out.println(strTen);
System.out.println(strTwo);
}
}
四、BigInteger不常見(jiàn)的成員方法
上面是我們經(jīng)常會(huì)使用的一些常見(jiàn)方法,當(dāng)然BigInteger中還有許多好用但不常見(jiàn)的方法,現(xiàn)整理分享給大家!
下面的方法都是指二進(jìn)制
| 方法名 | 含義 |
| public BigInteger shiftLeft?(int n) | 左移一個(gè)比特位,*2 |
| public BigInteger shiftRight?(int n) | 右移一個(gè)比特位,/2 |
| public BigInteger and?(BigInteger val) | &和 |
| public BigInteger or?(BigInteger val) | |或 |
| public BigInteger xor?(BigInteger val) | ^異或 |
| public BigInteger not() | ~取反 |
| public BigInteger andNot?(BigInteger val) | &~ 先取和再取反 |
| public boolean testBit?(int n) | 從0開(kāi)始,第n位如果是1,則返回true,否則位false ,必須是正數(shù) |
| public BigInteger setBit?(int n) | 將第n 位置1 |
| public BigInteger clearBit?(int n) | 將第n 位置0 |
| public BigInteger flipBit?(int n) | 如果第n為原來(lái)是1,則置0;如果第n為原來(lái)是0,則置1; |
| public int getLowestSetBit() | 尋找到第一個(gè)不為零數(shù)的 0的個(gè)數(shù)。如7->0111,則是4 |
| public int bitLength() | 返回位長(zhǎng),不包含符號(hào)位。如7->0111,則是3 |
| public int bitCount() | 補(bǔ)碼表中和符號(hào)位不同的個(gè)數(shù)。如7->0111,則是3 |
到此這篇關(guān)于關(guān)于Java大整數(shù)運(yùn)算之BigInteger類的文章就介紹到這了,更多相關(guān)Java大整數(shù)BigInteger類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Java?SpringBoot的前后端分離信息管理系統(tǒng)的設(shè)計(jì)和實(shí)現(xiàn)
當(dāng)今社會(huì),人才的流動(dòng)速度大大增加,因此也對(duì)黨建工作的管理層面工作帶來(lái)了空前且復(fù)雜的挑戰(zhàn),從而使得如何高效的開(kāi)展管理黨建工作成為了亟待解決的問(wèn)題。本文將介紹通過(guò)Java?SpringBoot實(shí)現(xiàn)前后端分離信息管理系統(tǒng),感興趣的同學(xué)可以了解一下2021-11-11
SpringCloud GateWay動(dòng)態(tài)路由用法
網(wǎng)關(guān)作為所有項(xiàng)目的入口,不希望重啟,因此動(dòng)態(tài)路由是必須的,動(dòng)態(tài)路由主要通過(guò)RouteDefinitionRepository接口實(shí)現(xiàn),其默認(rèn)的實(shí)現(xiàn)是InMemoryRouteDefinitionRepository,即在內(nèi)存中存儲(chǔ)路由配置,可基于這個(gè)map對(duì)象操作,動(dòng)態(tài)路由的實(shí)現(xiàn)方案有兩種2024-10-10
IDEA關(guān)于.properties資源文件的編碼調(diào)整問(wèn)題
這篇文章主要介紹了IDEA關(guān)于.properties資源文件的編碼調(diào)整問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Springboot注解之@EnableAutoConfiguration詳解
這篇文章主要介紹了Springboot注解之@EnableAutoConfiguration詳解,@EnableAutoConfiguration是一個(gè)加載Starter目錄包之外的需要Spring自動(dòng)生成bean對(duì)象,本文對(duì)其進(jìn)行總結(jié),需要的朋友可以參考下2023-08-08
實(shí)例講解Java的Spring框架中的控制反轉(zhuǎn)和依賴注入
這篇文章主要介紹了Java的Spring框架中的控制反轉(zhuǎn)和依賴注入,Spring是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下2016-02-02

