< prev index next >

src/java.base/share/classes/jdk/internal/math/FDBigInteger.java

Print this page
rev 16194 : imported patch XXXXXXX-Use-StringBuilder-appendN-method
   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1449         if (r < SMALL_5_POW.length) {
1450             return bigq.mult(SMALL_5_POW[r]);
1451         } else {
1452             return bigq.mult(big5powRec(r));
1453         }
1454     }
1455 
1456     // for debugging ...
1457     /**
1458      * Converts this <code>FDBigInteger</code> to a hexadecimal string.
1459      *
1460      * @return The hexadecimal string representation.
1461      */
1462     public String toHexString(){
1463         if(nWords ==0) {
1464             return "0";
1465         }
1466         StringBuilder sb = new StringBuilder((nWords +offset)*8);
1467         for(int i= nWords -1; i>=0; i--) {
1468             String subStr = Integer.toHexString(data[i]);
1469             for(int j = subStr.length(); j<8; j++) {
1470                 sb.append('0');
1471             }
1472             sb.append(subStr);
1473         }
1474         for(int i=offset; i>0; i--) {
1475             sb.append("00000000");
1476         }
1477         return sb.toString();
1478     }
1479 
1480     // for debugging ...
1481     /**
1482      * Converts this <code>FDBigInteger</code> to a <code>BigInteger</code>.
1483      *
1484      * @return The <code>BigInteger</code> representation.
1485      */
1486     public BigInteger toBigInteger() {
1487         byte[] magnitude = new byte[nWords * 4 + 1];
1488         for (int i = 0; i < nWords; i++) {
1489             int w = data[i];
1490             magnitude[magnitude.length - 4 * i - 1] = (byte) w;
1491             magnitude[magnitude.length - 4 * i - 2] = (byte) (w >> 8);
1492             magnitude[magnitude.length - 4 * i - 3] = (byte) (w >> 16);
1493             magnitude[magnitude.length - 4 * i - 4] = (byte) (w >> 24);
1494         }
1495         return new BigInteger(magnitude).shiftLeft(offset * 32);
1496     }
   1 /*
   2  * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1449         if (r < SMALL_5_POW.length) {
1450             return bigq.mult(SMALL_5_POW[r]);
1451         } else {
1452             return bigq.mult(big5powRec(r));
1453         }
1454     }
1455 
1456     // for debugging ...
1457     /**
1458      * Converts this <code>FDBigInteger</code> to a hexadecimal string.
1459      *
1460      * @return The hexadecimal string representation.
1461      */
1462     public String toHexString(){
1463         if(nWords ==0) {
1464             return "0";
1465         }
1466         StringBuilder sb = new StringBuilder((nWords +offset)*8);
1467         for(int i= nWords -1; i>=0; i--) {
1468             String subStr = Integer.toHexString(data[i]);
1469             if (subStr.length() < 8) {
1470                 sb.appendN('0', 8 - subStr.length());
1471             }
1472             sb.append(subStr);
1473         }
1474         sb.appendN('0', offset * 8);


1475         return sb.toString();
1476     }
1477 
1478     // for debugging ...
1479     /**
1480      * Converts this <code>FDBigInteger</code> to a <code>BigInteger</code>.
1481      *
1482      * @return The <code>BigInteger</code> representation.
1483      */
1484     public BigInteger toBigInteger() {
1485         byte[] magnitude = new byte[nWords * 4 + 1];
1486         for (int i = 0; i < nWords; i++) {
1487             int w = data[i];
1488             magnitude[magnitude.length - 4 * i - 1] = (byte) w;
1489             magnitude[magnitude.length - 4 * i - 2] = (byte) (w >> 8);
1490             magnitude[magnitude.length - 4 * i - 3] = (byte) (w >> 16);
1491             magnitude[magnitude.length - 4 * i - 4] = (byte) (w >> 24);
1492         }
1493         return new BigInteger(magnitude).shiftLeft(offset * 32);
1494     }
< prev index next >