Java中BigInteger類型


BigInteger是java.math包提供的處理大整數類型,實現了大整數的存儲,四則運算,判斷素數的方法,求冪,求模,求逆元,求最大公約數等方法。本文主要分析下BigInteger對於大整數的存儲和幾個常用函數的實現。

toByteArray函數實現:

    public byte[] toByteArray() {
        int byteLen = bitLength()/8 + 1;
        byte[] byteArray = new byte[byteLen];

//每次從mag數組中取出一個整數,低字節存在低地址,高字節存在高地址
for (int i=byteLen-1, bytesCopied=4, nextInt=0, intIndex=0; i >= 0; i--) { if (bytesCopied == 4) { nextInt = getInt(intIndex++); bytesCopied = 1; } else { nextInt >>>= 8; bytesCopied++; } byteArray[i] = (byte)nextInt; } return byteArray; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM