src/share/classes/java/lang/Long.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2011, 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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 


  28 /**
  29  * The {@code Long} class wraps a value of the primitive type {@code
  30  * long} in an object. An object of type {@code Long} contains a
  31  * single field whose type is {@code long}.
  32  *
  33  * <p> In addition, this class provides several methods for converting
  34  * a {@code long} to a {@code String} and a {@code String} to a {@code
  35  * long}, as well as other constants and methods useful when dealing
  36  * with a {@code long}.
  37  *
  38  * <p>Implementation note: The implementations of the "bit twiddling"
  39  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  40  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  41  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  42  * Delight</i>, (Addison Wesley, 2002).
  43  *
  44  * @author  Lee Boynton
  45  * @author  Arthur van Hoff
  46  * @author  Josh Bloch
  47  * @author  Joseph D. Darcy


 123         boolean negative = (i < 0);
 124 
 125         if (!negative) {
 126             i = -i;
 127         }
 128 
 129         while (i <= -radix) {
 130             buf[charPos--] = Integer.digits[(int)(-(i % radix))];
 131             i = i / radix;
 132         }
 133         buf[charPos] = Integer.digits[(int)(-i)];
 134 
 135         if (negative) {
 136             buf[--charPos] = '-';
 137         }
 138 
 139         return new String(buf, charPos, (65 - charPos));
 140     }
 141 
 142     /**













































































 143      * Returns a string representation of the {@code long}
 144      * argument as an unsigned integer in base&nbsp;16.
 145      *
 146      * <p>The unsigned {@code long} value is the argument plus
 147      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 148      * equal to the argument.  This value is converted to a string of
 149      * ASCII digits in hexadecimal (base&nbsp;16) with no extra
 150      * leading {@code 0}s.  If the unsigned magnitude is zero, it
 151      * is represented by a single zero character {@code '0'}
 152      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 153      * the representation of the unsigned magnitude will not be the
 154      * zero character. The following characters are used as
 155      * hexadecimal digits:






 156      *
 157      * <blockquote>
 158      *  {@code 0123456789abcdef}
 159      * </blockquote>
 160      *
 161      * These are the characters <code>'&#92;u0030'</code> through
 162      * <code>'&#92;u0039'</code> and  <code>'&#92;u0061'</code> through
 163      * <code>'&#92;u0066'</code>.  If uppercase letters are desired,
 164      * the {@link java.lang.String#toUpperCase()} method may be called
 165      * on the result:
 166      *
 167      * <blockquote>
 168      *  {@code Long.toHexString(n).toUpperCase()}
 169      * </blockquote>
 170      *
 171      * @param   i   a {@code long} to be converted to a string.
 172      * @return  the string representation of the unsigned {@code long}
 173      *          value represented by the argument in hexadecimal
 174      *          (base&nbsp;16).


 175      * @since   JDK 1.0.2
 176      */
 177     public static String toHexString(long i) {
 178         return toUnsignedString(i, 4);
 179     }
 180 
 181     /**
 182      * Returns a string representation of the {@code long}
 183      * argument as an unsigned integer in base&nbsp;8.
 184      *
 185      * <p>The unsigned {@code long} value is the argument plus
 186      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 187      * equal to the argument.  This value is converted to a string of
 188      * ASCII digits in octal (base&nbsp;8) with no extra leading
 189      * {@code 0}s.
 190      *





 191      * <p>If the unsigned magnitude is zero, it is represented by a
 192      * single zero character {@code '0'}
 193      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 194      * the representation of the unsigned magnitude will not be the
 195      * zero character. The following characters are used as octal
 196      * digits:
 197      *
 198      * <blockquote>
 199      *  {@code 01234567}
 200      * </blockquote>
 201      *
 202      * These are the characters <code>'&#92;u0030'</code> through
 203      * <code>'&#92;u0037'</code>.
 204      *
 205      * @param   i   a {@code long} to be converted to a string.
 206      * @return  the string representation of the unsigned {@code long}
 207      *          value represented by the argument in octal (base&nbsp;8).


 208      * @since   JDK 1.0.2
 209      */
 210     public static String toOctalString(long i) {
 211         return toUnsignedString(i, 3);
 212     }
 213 
 214     /**
 215      * Returns a string representation of the {@code long}
 216      * argument as an unsigned integer in base&nbsp;2.
 217      *
 218      * <p>The unsigned {@code long} value is the argument plus
 219      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 220      * equal to the argument.  This value is converted to a string of
 221      * ASCII digits in binary (base&nbsp;2) with no extra leading
 222      * {@code 0}s.  If the unsigned magnitude is zero, it is
 223      * represented by a single zero character {@code '0'}
 224      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 225      * the representation of the unsigned magnitude will not be the
 226      * zero character. The characters {@code '0'}
 227      * (<code>'&#92;u0030'</code>) and {@code '1'}
 228      * (<code>'&#92;u0031'</code>) are used as binary digits.






 229      *
 230      * @param   i   a {@code long} to be converted to a string.
 231      * @return  the string representation of the unsigned {@code long}
 232      *          value represented by the argument in binary (base&nbsp;2).


 233      * @since   JDK 1.0.2
 234      */
 235     public static String toBinaryString(long i) {
 236         return toUnsignedString(i, 1);
 237     }
 238 
 239     /**
 240      * Convert the integer to an unsigned number.
 241      */
 242     private static String toUnsignedString(long i, int shift) {
 243         char[] buf = new char[64];
 244         int charPos = 64;
 245         int radix = 1 << shift;
 246         long mask = radix - 1;
 247         do {
 248             buf[--charPos] = Integer.digits[(int)(i & mask)];
 249             i >>>= shift;
 250         } while (i != 0);
 251         return new String(buf, charPos, (64 - charPos));
 252     }
 253 
 254     /**
 255      * Returns a {@code String} object representing the specified
 256      * {@code long}.  The argument is converted to signed decimal
 257      * representation and returned as a string, exactly as if the
 258      * argument and the radix 10 were given as arguments to the {@link
 259      * #toString(long, int)} method.
 260      *
 261      * @param   i   a {@code long} to be converted.
 262      * @return  a string representation of the argument in base&nbsp;10.
 263      */
 264     public static String toString(long i) {
 265         if (i == Long.MIN_VALUE)
 266             return "-9223372036854775808";
 267         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 268         char[] buf = new char[size];
 269         getChars(i, size, buf);
 270         return new String(0, size, buf);
 271     }
 272 
 273     /**

















 274      * Places characters representing the integer i into the
 275      * character array buf. The characters are placed into
 276      * the buffer backwards starting with the least significant
 277      * digit at the specified index (exclusive), and working
 278      * backwards from there.
 279      *
 280      * Will fail if i == Long.MIN_VALUE
 281      */
 282     static void getChars(long i, int index, char[] buf) {
 283         long q;
 284         int r;
 285         int charPos = index;
 286         char sign = 0;
 287 
 288         if (i < 0) {
 289             sign = '-';
 290             i = -i;
 291         }
 292 
 293         // Get 2 digits/iteration using longs until quotient fits into an int


 468      * #parseLong(java.lang.String, int)} method.
 469      *
 470      * <p>Note that neither the character {@code L}
 471      * (<code>'&#92;u004C'</code>) nor {@code l}
 472      * (<code>'&#92;u006C'</code>) is permitted to appear at the end
 473      * of the string as a type indicator, as would be permitted in
 474      * Java programming language source code.
 475      *
 476      * @param      s   a {@code String} containing the {@code long}
 477      *             representation to be parsed
 478      * @return     the {@code long} represented by the argument in
 479      *             decimal.
 480      * @throws     NumberFormatException  if the string does not contain a
 481      *             parsable {@code long}.
 482      */
 483     public static long parseLong(String s) throws NumberFormatException {
 484         return parseLong(s, 10);
 485     }
 486 
 487     /**



































































































 488      * Returns a {@code Long} object holding the value
 489      * extracted from the specified {@code String} when parsed
 490      * with the radix given by the second argument.  The first
 491      * argument is interpreted as representing a signed
 492      * {@code long} in the radix specified by the second
 493      * argument, exactly as if the arguments were given to the {@link
 494      * #parseLong(java.lang.String, int)} method. The result is a
 495      * {@code Long} object that represents the {@code long}
 496      * value specified by the string.
 497      *
 498      * <p>In other words, this method returns a {@code Long} object equal
 499      * to the value of:
 500      *
 501      * <blockquote>
 502      *  {@code new Long(Long.parseLong(s, radix))}
 503      * </blockquote>
 504      *
 505      * @param      s       the string to be parsed
 506      * @param      radix   the radix to be used in interpreting {@code s}
 507      * @return     a {@code Long} object holding the value


 960     }
 961 
 962     /**
 963      * Compares two {@code long} values numerically.
 964      * The value returned is identical to what would be returned by:
 965      * <pre>
 966      *    Long.valueOf(x).compareTo(Long.valueOf(y))
 967      * </pre>
 968      *
 969      * @param  x the first {@code long} to compare
 970      * @param  y the second {@code long} to compare
 971      * @return the value {@code 0} if {@code x == y};
 972      *         a value less than {@code 0} if {@code x < y}; and
 973      *         a value greater than {@code 0} if {@code x > y}
 974      * @since 1.7
 975      */
 976     public static int compare(long x, long y) {
 977         return (x < y) ? -1 : ((x == y) ? 0 : 1);
 978     }
 979 


































































 980 
 981     // Bit Twiddling
 982 
 983     /**
 984      * The number of bits used to represent a {@code long} value in two's
 985      * complement binary form.
 986      *
 987      * @since 1.5
 988      */
 989     public static final int SIZE = 64;
 990 
 991     /**
 992      * Returns a {@code long} value with at most a single one-bit, in the
 993      * position of the highest-order ("leftmost") one-bit in the specified
 994      * {@code long} value.  Returns zero if the specified value has no
 995      * one-bits in its two's complement binary representation, that is, if it
 996      * is equal to zero.
 997      *
 998      * @return a {@code long} value with a single one-bit, in the position
 999      *     of the highest-order one-bit in the specified value, or zero if


   1 /*
   2  * Copyright (c) 1994, 2012, 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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.math.*;
  29 
  30 /**
  31  * The {@code Long} class wraps a value of the primitive type {@code
  32  * long} in an object. An object of type {@code Long} contains a
  33  * single field whose type is {@code long}.
  34  *
  35  * <p> In addition, this class provides several methods for converting
  36  * a {@code long} to a {@code String} and a {@code String} to a {@code
  37  * long}, as well as other constants and methods useful when dealing
  38  * with a {@code long}.
  39  *
  40  * <p>Implementation note: The implementations of the "bit twiddling"
  41  * methods (such as {@link #highestOneBit(long) highestOneBit} and
  42  * {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are
  43  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  44  * Delight</i>, (Addison Wesley, 2002).
  45  *
  46  * @author  Lee Boynton
  47  * @author  Arthur van Hoff
  48  * @author  Josh Bloch
  49  * @author  Joseph D. Darcy


 125         boolean negative = (i < 0);
 126 
 127         if (!negative) {
 128             i = -i;
 129         }
 130 
 131         while (i <= -radix) {
 132             buf[charPos--] = Integer.digits[(int)(-(i % radix))];
 133             i = i / radix;
 134         }
 135         buf[charPos] = Integer.digits[(int)(-i)];
 136 
 137         if (negative) {
 138             buf[--charPos] = '-';
 139         }
 140 
 141         return new String(buf, charPos, (65 - charPos));
 142     }
 143 
 144     /**
 145      * Returns an unsigned string representation of the first argument
 146      * in the radix specified by the second argument.
 147      *
 148      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
 149      * or larger than {@code Character.MAX_RADIX}, then the radix
 150      * {@code 10} is used instead.
 151      *
 152      * <p>Note that since the first argument is treated as an unsigned
 153      * value, no leading sign character is printed.
 154      *
 155      * <p>If the magnitude is zero, it is represented by a single zero
 156      * character {@code '0'} (<code>'&#92;u0030'</code>); otherwise,
 157      * the first character of the representation of the magnitude will
 158      * not be the zero character.
 159      *
 160      * <p>The characters used as digits and the behavior of radixes
 161      * is the same as {@link #toString(long, int) toString}.
 162      *
 163      * @param   i       an integer to be converted to an unsigned string.
 164      * @param   radix   the radix to use in the string representation.
 165      * @return  an unsigned string representation of the argument in the specified radix.
 166      * @see     #toString(long, int)
 167      * @since 1.8
 168      */
 169     public static String toUnsignedString(long i, int radix) {
 170         if (i >= 0)
 171             return toString(i, radix);
 172         else {
 173             switch (radix) {
 174             case 2:
 175                 return toBinaryString(i);
 176 
 177             case 4:
 178                 return toUnsignedString0(i, 2);
 179 
 180             case 8:
 181                 return toOctalString(i);
 182 
 183             case 10:
 184                 /*
 185                  * We can get the effect of an unsigned division by 10
 186                  * on a long value by first shifting right, yielding a
 187                  * positive value, and then dividing by 5.  This
 188                  * allows the last digit and preceding digits to be
 189                  * isolated more quickly than by an initial conversion
 190                  * to BigInteger.
 191                  */
 192                 long quot = (i >>> 1) / 5;
 193                 long rem = i - quot * 10;
 194                 return toString(quot) + rem;
 195 
 196             case 16:
 197                 return toHexString(i);
 198 
 199             case 32:
 200                 return toUnsignedString0(i, 5);
 201 
 202             default:
 203                 return toUnsignedBigInteger(i).toString(radix);
 204             }
 205         }
 206     }
 207 
 208     /**
 209      * Return a BigInteger equal to the unsigned value of the
 210      * argument.
 211      */
 212     private static BigInteger toUnsignedBigInteger(long i) {
 213         int upper = (int) ((i >> 32) & 0xffffffff);
 214         int lower = (int) i;
 215 
 216          // return (upper << 32) + lower
 217         return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
 218             add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
 219     }
 220 
 221     /**
 222      * Returns a string representation of the {@code long}
 223      * argument as an unsigned integer in base&nbsp;16.
 224      *
 225      * <p>The unsigned {@code long} value is the argument plus
 226      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 227      * equal to the argument.  This value is converted to a string of
 228      * ASCII digits in hexadecimal (base&nbsp;16) with no extra
 229      * leading {@code 0}s.
 230      *
 231      * <p>The value of the argument can be recovered from the returned
 232      * string {@code s} by calling {@link
 233      * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
 234      * 16)}.
 235      *
 236      * <p>If the unsigned magnitude is zero, it is represented by a
 237      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 238      * otherwise, the first character of the representation of the
 239      * unsigned magnitude will not be the zero character. The
 240      * following characters are used as hexadecimal digits:
 241      *
 242      * <blockquote>
 243      *  {@code 0123456789abcdef}
 244      * </blockquote>
 245      *
 246      * These are the characters <code>'&#92;u0030'</code> through
 247      * <code>'&#92;u0039'</code> and  <code>'&#92;u0061'</code> through
 248      * <code>'&#92;u0066'</code>.  If uppercase letters are desired,
 249      * the {@link java.lang.String#toUpperCase()} method may be called
 250      * on the result:
 251      *
 252      * <blockquote>
 253      *  {@code Long.toHexString(n).toUpperCase()}
 254      * </blockquote>
 255      *
 256      * @param   i   a {@code long} to be converted to a string.
 257      * @return  the string representation of the unsigned {@code long}
 258      *          value represented by the argument in hexadecimal
 259      *          (base&nbsp;16).
 260      * @see #parseUnsignedLong(String, int)
 261      * @see #toUnsignedString(long, int)
 262      * @since   JDK 1.0.2
 263      */
 264     public static String toHexString(long i) {
 265         return toUnsignedString0(i, 4);
 266     }
 267 
 268     /**
 269      * Returns a string representation of the {@code long}
 270      * argument as an unsigned integer in base&nbsp;8.
 271      *
 272      * <p>The unsigned {@code long} value is the argument plus
 273      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 274      * equal to the argument.  This value is converted to a string of
 275      * ASCII digits in octal (base&nbsp;8) with no extra leading
 276      * {@code 0}s.
 277      *
 278      * <p>The value of the argument can be recovered from the returned
 279      * string {@code s} by calling {@link
 280      * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
 281      * 8)}.
 282      *
 283      * <p>If the unsigned magnitude is zero, it is represented by a
 284      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 285      * otherwise, the first character of the representation of the
 286      * unsigned magnitude will not be the zero character. The
 287      * following characters are used as octal digits:

 288      *
 289      * <blockquote>
 290      *  {@code 01234567}
 291      * </blockquote>
 292      *
 293      * These are the characters <code>'&#92;u0030'</code> through
 294      * <code>'&#92;u0037'</code>.
 295      *
 296      * @param   i   a {@code long} to be converted to a string.
 297      * @return  the string representation of the unsigned {@code long}
 298      *          value represented by the argument in octal (base&nbsp;8).
 299      * @see #parseUnsignedLong(String, int)
 300      * @see #toUnsignedString(long, int)
 301      * @since   JDK 1.0.2
 302      */
 303     public static String toOctalString(long i) {
 304         return toUnsignedString0(i, 3);
 305     }
 306 
 307     /**
 308      * Returns a string representation of the {@code long}
 309      * argument as an unsigned integer in base&nbsp;2.
 310      *
 311      * <p>The unsigned {@code long} value is the argument plus
 312      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 313      * equal to the argument.  This value is converted to a string of
 314      * ASCII digits in binary (base&nbsp;2) with no extra leading
 315      * {@code 0}s.
 316      *
 317      * <p>The value of the argument can be recovered from the returned
 318      * string {@code s} by calling {@link
 319      * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
 320      * 2)}.
 321      *
 322      * <p>If the unsigned magnitude is zero, it is represented by a
 323      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 324      * otherwise, the first character of the representation of the
 325      * unsigned magnitude will not be the zero character. The
 326      * characters {@code '0'} (<code>'&#92;u0030'</code>) and {@code
 327      * '1'} (<code>'&#92;u0031'</code>) are used as binary digits.
 328      *
 329      * @param   i   a {@code long} to be converted to a string.
 330      * @return  the string representation of the unsigned {@code long}
 331      *          value represented by the argument in binary (base&nbsp;2).
 332      * @see #parseUnsignedLong(String, int)
 333      * @see #toUnsignedString(long, int)
 334      * @since   JDK 1.0.2
 335      */
 336     public static String toBinaryString(long i) {
 337         return toUnsignedString0(i, 1);
 338     }
 339 
 340     /**
 341      * Convert the integer to an unsigned number.
 342      */
 343     private static String toUnsignedString0(long i, int shift) {
 344         char[] buf = new char[64];
 345         int charPos = 64;
 346         int radix = 1 << shift;
 347         long mask = radix - 1;
 348         do {
 349             buf[--charPos] = Integer.digits[(int)(i & mask)];
 350             i >>>= shift;
 351         } while (i != 0);
 352         return new String(buf, charPos, (64 - charPos));
 353     }
 354 
 355     /**
 356      * Returns a {@code String} object representing the specified
 357      * {@code long}.  The argument is converted to signed decimal
 358      * representation and returned as a string, exactly as if the
 359      * argument and the radix 10 were given as arguments to the {@link
 360      * #toString(long, int)} method.
 361      *
 362      * @param   i   a {@code long} to be converted.
 363      * @return  a string representation of the argument in base&nbsp;10.
 364      */
 365     public static String toString(long i) {
 366         if (i == Long.MIN_VALUE)
 367             return "-9223372036854775808";
 368         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 369         char[] buf = new char[size];
 370         getChars(i, size, buf);
 371         return new String(0, size, buf);
 372     }
 373 
 374     /**
 375      * Returns an unsigned string representation of the argument.
 376      *
 377      * The argument is converted to unsigned decimal representation
 378      * and returned as a string exactly as if the argument and radix
 379      * 10 were given as arguments to the {@link #toUnsignedString(long,
 380      * int)} method.
 381      *
 382      * @param   i  an integer to be converted to an unsigned string.
 383      * @return  an unsigned string representation of the argument.
 384      * @see     #toUnsignedString(long, int)
 385      * @since 1.8
 386      */
 387     public static String toUnsignedString(long i) {
 388         return toUnsignedString(i, 10);
 389     }
 390 
 391     /**
 392      * Places characters representing the integer i into the
 393      * character array buf. The characters are placed into
 394      * the buffer backwards starting with the least significant
 395      * digit at the specified index (exclusive), and working
 396      * backwards from there.
 397      *
 398      * Will fail if i == Long.MIN_VALUE
 399      */
 400     static void getChars(long i, int index, char[] buf) {
 401         long q;
 402         int r;
 403         int charPos = index;
 404         char sign = 0;
 405 
 406         if (i < 0) {
 407             sign = '-';
 408             i = -i;
 409         }
 410 
 411         // Get 2 digits/iteration using longs until quotient fits into an int


 586      * #parseLong(java.lang.String, int)} method.
 587      *
 588      * <p>Note that neither the character {@code L}
 589      * (<code>'&#92;u004C'</code>) nor {@code l}
 590      * (<code>'&#92;u006C'</code>) is permitted to appear at the end
 591      * of the string as a type indicator, as would be permitted in
 592      * Java programming language source code.
 593      *
 594      * @param      s   a {@code String} containing the {@code long}
 595      *             representation to be parsed
 596      * @return     the {@code long} represented by the argument in
 597      *             decimal.
 598      * @throws     NumberFormatException  if the string does not contain a
 599      *             parsable {@code long}.
 600      */
 601     public static long parseLong(String s) throws NumberFormatException {
 602         return parseLong(s, 10);
 603     }
 604 
 605     /**
 606      * Parses the string argument as an unsigned integer in the radix
 607      * specified by the second argument.
 608      *
 609      * The characters in the string must all be digits of the
 610      * specified radix (as determined by whether {@link
 611      * java.lang.Character#digit(char, int)} returns a nonnegative
 612      * value), except that the first character may be an ASCII plus
 613      * sign {@code '+'} (<code>'&#92;u002B'</code>). The resulting
 614      * integer value is returned.
 615      *
 616      * <p>An exception of type {@code NumberFormatException} is
 617      * thrown if any of the following situations occurs:
 618      * <ul>
 619      * <li>The first argument is {@code null} or is a string of
 620      * length zero.
 621      *
 622      * <li>The radix is either smaller than
 623      * {@link java.lang.Character#MIN_RADIX} or
 624      * larger than {@link java.lang.Character#MAX_RADIX}.
 625      *
 626      * <li>Any character of the string is not a digit of the specified
 627      * radix, except that the first character may be a plus sign
 628      * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
 629      * string is longer than length 1.
 630      *
 631      * <li>The value represented by the string is larger than the
 632      * largest unsigned {@code long}, 2<sup>64</sup>-1.
 633      *
 634      * </ul>
 635      *
 636      *
 637      * @param      s   the {@code String} containing the unsigned integer
 638      *                  representation to be parsed
 639      * @param      radix   the radix to be used while parsing {@code s}.
 640      * @return     the integer represented by the string argument in the
 641      *             specified radix.
 642      * @throws     NumberFormatException if the {@code String}
 643      *             does not contain a parsable {@code long}.
 644      * @since 1.8
 645      */
 646     public static long parseUnsignedLong(String s, int radix)
 647                 throws NumberFormatException {
 648         if (s == null)  {
 649             throw new NumberFormatException("null");
 650         }
 651 
 652         int len = s.length();
 653         if (len > 0) {
 654             char firstChar = s.charAt(0);
 655             if (firstChar == '-') {
 656                 throw new 
 657                     NumberFormatException(String.format("Illegal leading minus sign " +
 658                                                        "on unsigned string %s.", s));
 659             } else {
 660                 if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits
 661                     (radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits
 662                     return parseLong(s, radix); 
 663                 }
 664 
 665                 // For simplicty, use BigInteger for parsing
 666                 BigInteger bi = new BigInteger(s, radix);
 667 
 668                 // Largest *unsigned* value is all ones in binary
 669                 BigInteger limit = toUnsignedBigInteger(-1L);
 670                 
 671                 if (bi.compareTo(limit) <= 0) {
 672                     return bi.longValue();
 673                 } else {
 674                     throw new
 675                     NumberFormatException(String.format("String value %s exceeds " + 
 676                                                         "range of unsigned long.", s));
 677                 }
 678             }
 679         } else {
 680             throw NumberFormatException.forInputString(s);
 681         }
 682     }
 683 
 684     /**
 685      * Parses the string argument as an unsigned decimal integer. The
 686      * characters in the string must all be decimal digits, except
 687      * that the first character may be an an ASCII plus sign {@code
 688      * '+'} (<code>'&#92;u002B'</code>). The resulting integer value
 689      * is returned, exactly as if the argument and the radix 10 were
 690      * given as arguments to the {@link
 691      * #parseUnsignedLong(java.lang.String, int)} method.
 692      *
 693      * @param s   a {@code String} containing the unsigned {@code long}
 694      *            representation to be parsed
 695      * @return    the unsigned integer value represented by the argument in decimal.
 696      * @throws    NumberFormatException  if the string does not contain a
 697      *            parsable unsigned integer.
 698      * @since 1.8
 699      */
 700     public static long parseUnsignedLong(String s) throws NumberFormatException {
 701         return parseUnsignedLong(s, 10);
 702     }
 703 
 704     /**
 705      * Returns a {@code Long} object holding the value
 706      * extracted from the specified {@code String} when parsed
 707      * with the radix given by the second argument.  The first
 708      * argument is interpreted as representing a signed
 709      * {@code long} in the radix specified by the second
 710      * argument, exactly as if the arguments were given to the {@link
 711      * #parseLong(java.lang.String, int)} method. The result is a
 712      * {@code Long} object that represents the {@code long}
 713      * value specified by the string.
 714      *
 715      * <p>In other words, this method returns a {@code Long} object equal
 716      * to the value of:
 717      *
 718      * <blockquote>
 719      *  {@code new Long(Long.parseLong(s, radix))}
 720      * </blockquote>
 721      *
 722      * @param      s       the string to be parsed
 723      * @param      radix   the radix to be used in interpreting {@code s}
 724      * @return     a {@code Long} object holding the value


1177     }
1178 
1179     /**
1180      * Compares two {@code long} values numerically.
1181      * The value returned is identical to what would be returned by:
1182      * <pre>
1183      *    Long.valueOf(x).compareTo(Long.valueOf(y))
1184      * </pre>
1185      *
1186      * @param  x the first {@code long} to compare
1187      * @param  y the second {@code long} to compare
1188      * @return the value {@code 0} if {@code x == y};
1189      *         a value less than {@code 0} if {@code x < y}; and
1190      *         a value greater than {@code 0} if {@code x > y}
1191      * @since 1.7
1192      */
1193     public static int compare(long x, long y) {
1194         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1195     }
1196 
1197     /**
1198      * Compares two {@code long} values numerically treating the values
1199      * as unsigned.
1200      *
1201      * @param  x the first {@code long} to compare
1202      * @param  y the second {@code long} to compare
1203      * @return the value {@code 0} if {@code x == y}; a value less
1204      *         than {@code 0} if {@code x < y} as unsigned values; and
1205      *         a value greater than {@code 0} if {@code x > y} as
1206      *         unsigned values
1207      * @since 1.8
1208      */
1209     public static int compareUnsigned(long x, long y) {
1210         return compare(x + MIN_VALUE, y + MIN_VALUE);
1211     }
1212 
1213 
1214     /**
1215      * Returns the unsigned quotient of dividing the first argument by
1216      * the second where each argument is interpreted as an unsigned
1217      * value.
1218      *
1219      * In other words, return the unsigned value of {@code
1220      * (dividend / divisor)}.
1221      *
1222      * @return the unsigned quotient of the first argument divided by
1223      * the second argument
1224      * @param dividend the value to be divided
1225      * @param divisor the value doing the dividing
1226      * @since 1.8
1227      */
1228     public static long divideUnsigned(long dividend, long divisor) {
1229         if (divisor < 0L) { // signed comparison
1230             // Answer must be 0 or 1 depending on relative magnitude
1231             // of dividiend and divisor.
1232             return (compareUnsigned(dividend, divisor)) < 0 ? 0L :1L;
1233         }
1234 
1235         /*
1236          * For simple code, leveraging BigInteger.  Longer and faster
1237          * code written directly in terms of operations on longs is
1238          * possible; see "Hacker's Delight" for divide and remainder
1239          * algorithms.
1240          */
1241         return toUnsignedBigInteger(dividend).
1242             divide(toUnsignedBigInteger(divisor)).longValue();
1243     }
1244 
1245     /**
1246      * Returns the unsigned remainder from dividing the first argument by
1247      * the second where each argument is interpreted as an unsigned
1248      * value.
1249      *
1250      * In other words, return the unsigned value of {@code
1251      * (dividend % divisor)}.
1252      *
1253      * @return the unsigned remainder of the first argument divided by
1254      * the second argument
1255      * @param dividend the value to be divided
1256      * @param divisor the value doing the dividing
1257      * @since 1.8
1258      */
1259     public static long remainderUnsigned(long dividend, long divisor) {
1260         return toUnsignedBigInteger(dividend).
1261             remainder(toUnsignedBigInteger(divisor)).longValue();
1262     }
1263 
1264     // Bit Twiddling
1265 
1266     /**
1267      * The number of bits used to represent a {@code long} value in two's
1268      * complement binary form.
1269      *
1270      * @since 1.5
1271      */
1272     public static final int SIZE = 64;
1273 
1274     /**
1275      * Returns a {@code long} value with at most a single one-bit, in the
1276      * position of the highest-order ("leftmost") one-bit in the specified
1277      * {@code long} value.  Returns zero if the specified value has no
1278      * one-bits in its two's complement binary representation, that is, if it
1279      * is equal to zero.
1280      *
1281      * @return a {@code long} value with a single one-bit, in the position
1282      *     of the highest-order one-bit in the specified value, or zero if