BigInteger存資料的方式

2021-08-18 01:25:52 字數 868 閱讀 4078

問題源於乙個面試問題,當乙個long型別值乘以乙個long型別的值,需要用什麼來接收,當時第一時間想到用biginteger型別來接收,但是面試官的問題並不是說讓用封裝完成的類來接收,一時語塞!!!說下biginteger的存資料的方式,也沒有答上來,回來研究biginteger的底層操作才明白。

其實在biginteger中是使用int型別的陣列來儲存資料的,在原始碼中分析看到,這些int型別的資料也是通過二進位制數字來實現的,

private 

biginteger(long val) else

int highword = (int)(val >>> 32);

if (highword == 0) else

}

val是long型別的資料,共有8個位元組,high word為前四個位元組,儲存到mag[0],後四個位元組儲存到mag[1]裡面,從這個原始碼中我們也可以看出來,long數值要想獲得後四個位元組,直接轉型為int就可以了。int的最大值為:2147483647。也就是mag裡面可以放入2147483647個int值,每個int值為32位,mag可以表示的陣列範圍為:[-2

2147483647*32-1

,22147483647*32-1

-1]其構造方法有很多,例如: biginteger(string val),

將 biginteger 的十進位制字串表示形式轉換為 biginteger。

biginteger(string val, int radix)

將指定基數的 biginteger 的字串表示形式轉換為 biginteger。

如要將int型的2轉換為biginteger型,要寫為biginteger two=new biginteger("2");

BigInteger的使用解決資料溢位

biginteger 可以用來解決資料的溢位問題!下面我總結幾種關於biginteger的常用用法 1。probableprime和nextprobableprime。判斷質數,並返回 biginteger.probableprime int x 返回有可能是素數 質數 具有指定長度的正數bigin...

Redis存讀取資料

redis中包括四種資料型別,strings,lists,sets,sorted sets 接下來我們一一看這四種型別的用法 1.連線redis伺服器 redisclient client private void button1 click object sender,eventargs e 2....

向redis存資料和取資料

redistemplate使用方法歸納 redistemplate操作redis資料 這裡只是寫了我自己專案中用到的,具體redistemplate操作redis資料的內容見上面鏈結 1.向pom.xml中匯入依賴 dependency 2.向redis中存入資料 autowired private...