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 a string representation of the first argument as an
 146      * unsigned string 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 behavior of radixes and the characters used as digits
 161      * are 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         if (i >= 0L)
 214             return BigInteger.valueOf(i);
 215         else {
 216             int upper = (int) (i >>> 32);
 217             int lower = (int) i;
 218 
 219             // return (upper << 32) + lower
 220             return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
 221                 add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
 222         }
 223     }
 224 
 225     /**
 226      * Returns a string representation of the {@code long}
 227      * argument as an unsigned integer in base&nbsp;16.
 228      *
 229      * <p>The unsigned {@code long} value is the argument plus
 230      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 231      * equal to the argument.  This value is converted to a string of
 232      * ASCII digits in hexadecimal (base&nbsp;16) with no extra
 233      * leading {@code 0}s.
 234      *
 235      * <p>The value of the argument can be recovered from the returned
 236      * string {@code s} by calling {@link
 237      * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
 238      * 16)}.
 239      *
 240      * <p>If the unsigned magnitude is zero, it is represented by a
 241      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 242      * otherwise, the first character of the representation of the
 243      * unsigned magnitude will not be the zero character. The
 244      * following characters are used as hexadecimal digits:
 245      *
 246      * <blockquote>
 247      *  {@code 0123456789abcdef}
 248      * </blockquote>
 249      *
 250      * These are the characters <code>'&#92;u0030'</code> through
 251      * <code>'&#92;u0039'</code> and  <code>'&#92;u0061'</code> through
 252      * <code>'&#92;u0066'</code>.  If uppercase letters are desired,
 253      * the {@link java.lang.String#toUpperCase()} method may be called
 254      * on the result:
 255      *
 256      * <blockquote>
 257      *  {@code Long.toHexString(n).toUpperCase()}
 258      * </blockquote>
 259      *
 260      * @param   i   a {@code long} to be converted to a string.
 261      * @return  the string representation of the unsigned {@code long}
 262      *          value represented by the argument in hexadecimal
 263      *          (base&nbsp;16).
 264      * @see #parseUnsignedLong(String, int)
 265      * @see #toUnsignedString(long, int)
 266      * @since   JDK 1.0.2
 267      */
 268     public static String toHexString(long i) {
 269         return toUnsignedString0(i, 4);
 270     }
 271 
 272     /**
 273      * Returns a string representation of the {@code long}
 274      * argument as an unsigned integer in base&nbsp;8.
 275      *
 276      * <p>The unsigned {@code long} value is the argument plus
 277      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 278      * equal to the argument.  This value is converted to a string of
 279      * ASCII digits in octal (base&nbsp;8) with no extra leading
 280      * {@code 0}s.
 281      *
 282      * <p>The value of the argument can be recovered from the returned
 283      * string {@code s} by calling {@link
 284      * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s,
 285      * 8)}.
 286      *
 287      * <p>If the unsigned magnitude is zero, it is represented by a
 288      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 289      * otherwise, the first character of the representation of the
 290      * unsigned magnitude will not be the zero character. The
 291      * following characters are used as octal digits:

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


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


1198     }
1199 
1200     /**
1201      * Compares two {@code long} values numerically.
1202      * The value returned is identical to what would be returned by:
1203      * <pre>
1204      *    Long.valueOf(x).compareTo(Long.valueOf(y))
1205      * </pre>
1206      *
1207      * @param  x the first {@code long} to compare
1208      * @param  y the second {@code long} to compare
1209      * @return the value {@code 0} if {@code x == y};
1210      *         a value less than {@code 0} if {@code x < y}; and
1211      *         a value greater than {@code 0} if {@code x > y}
1212      * @since 1.7
1213      */
1214     public static int compare(long x, long y) {
1215         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1216     }
1217 
1218     /**
1219      * Compares two {@code long} values numerically treating the values
1220      * as unsigned.
1221      *
1222      * @param  x the first {@code long} to compare
1223      * @param  y the second {@code long} to compare
1224      * @return the value {@code 0} if {@code x == y}; a value less
1225      *         than {@code 0} if {@code x < y} as unsigned values; and
1226      *         a value greater than {@code 0} if {@code x > y} as
1227      *         unsigned values
1228      * @since 1.8
1229      */
1230     public static int compareUnsigned(long x, long y) {
1231         return compare(x + MIN_VALUE, y + MIN_VALUE);
1232     }
1233 
1234 
1235     /**
1236      * Returns the unsigned quotient of dividing the first argument by
1237      * the second where each argument and the result is interpreted as
1238      * an unsigned value.
1239      *
1240      * <p>Note that in two's complement arithmetic, the three other
1241      * basic arithmetic operations of add, subtract, and multiply are
1242      * bit-wise identical if the two operands are regarded as both
1243      * being signed or both being unsigned.  Therefore separate {@code
1244      * addUnsigned}, etc. methods are not provided.
1245      *
1246      * @return the unsigned quotient of the first argument divided by
1247      * the second argument
1248      * @param dividend the value to be agnitude
1249             // of dividend and divisor.divided
1250      * @param divisor the value doing the dividing
1251      * @see #remainderUnsigned
1252      * @since 1.8
1253      */
1254     public static long divideUnsigned(long dividend, long divisor) {
1255         if (divisor < 0L) { // signed comparison
1256             // Answer must be 0 or 1 depending on relative magnitude
1257             // of dividend and divisor.
1258             return (compareUnsigned(dividend, divisor)) < 0 ? 0L :1L;
1259         }
1260 
1261         if (dividend > 0) //  Both inputs non-negative
1262             return dividend/divisor;
1263         else {
1264             /*
1265              * For simple code, leveraging BigInteger.  Longer and faster
1266              * code written directly in terms of operations on longs is
1267              * possible; see "Hacker's Delight" for divide and remainder
1268              * algorithms.
1269              */
1270             return toUnsignedBigInteger(dividend).
1271                 divide(toUnsignedBigInteger(divisor)).longValue();
1272         }
1273     }
1274 
1275     /**
1276      * Returns the unsigned remainder from dividing the first argument
1277      * by the second where each argument and the result is interpreted
1278      * as an unsigned value.
1279      *
1280      * @return the unsigned remainder of the first argument divided by
1281      * the second argument
1282      * @param dividend the value to be divided
1283      * @param divisor the value doing the dividing
1284      * @see #divideUnsigned
1285      * @since 1.8
1286      */
1287     public static long remainderUnsigned(long dividend, long divisor) {
1288         if (dividend > 0 && divisor > 0) { // signed comparisons
1289             return dividend % divisor;
1290         } else {
1291             if (compareUnsigned(dividend, divisor) < 0) // Avoid explicit check for 0 divisor
1292                 return dividend;
1293             else
1294                 return toUnsignedBigInteger(dividend).
1295                     remainder(toUnsignedBigInteger(divisor)).longValue();
1296         }
1297     }
1298 
1299     // Bit Twiddling
1300 
1301     /**
1302      * The number of bits used to represent a {@code long} value in two's
1303      * complement binary form.
1304      *
1305      * @since 1.5
1306      */
1307     public static final int SIZE = 64;
1308 
1309     /**
1310      * Returns a {@code long} value with at most a single one-bit, in the
1311      * position of the highest-order ("leftmost") one-bit in the specified
1312      * {@code long} value.  Returns zero if the specified value has no
1313      * one-bits in its two's complement binary representation, that is, if it
1314      * is equal to zero.
1315      *
1316      * @return a {@code long} value with a single one-bit, in the position
1317      *     of the highest-order one-bit in the specified value, or zero if