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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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


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














































 142      * Returns a string representation of the {@code long}
 143      * argument as an unsigned integer in base&nbsp;16.
 144      *
 145      * <p>The unsigned {@code long} value is the argument plus
 146      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 147      * equal to the argument.  This value is converted to a string of
 148      * ASCII digits in hexadecimal (base&nbsp;16) with no extra
 149      * leading {@code 0}s.  If the unsigned magnitude is zero, it
 150      * is represented by a single zero character {@code '0'}
 151      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 152      * the representation of the unsigned magnitude will not be the
 153      * zero character. The following characters are used as
 154      * hexadecimal digits:
 155      *
 156      * <blockquote>
 157      *  {@code 0123456789abcdef}
 158      * </blockquote>
 159      *
 160      * These are the characters <code>'&#92;u0030'</code> through
 161      * <code>'&#92;u0039'</code> and  <code>'&#92;u0061'</code> through
 162      * <code>'&#92;u0066'</code>.  If uppercase letters are desired,
 163      * the {@link java.lang.String#toUpperCase()} method may be called
 164      * on the result:
 165      *
 166      * <blockquote>
 167      *  {@code Long.toHexString(n).toUpperCase()}
 168      * </blockquote>
 169      *
 170      * @param   i   a {@code long} to be converted to a string.
 171      * @return  the string representation of the unsigned {@code long}
 172      *          value represented by the argument in hexadecimal
 173      *          (base&nbsp;16).
 174      * @since   JDK 1.0.2
 175      */
 176     public static String toHexString(long i) {
 177         return toUnsignedString(i, 4);
 178     }
 179 
 180     /**
 181      * Returns a string representation of the {@code long}
 182      * argument as an unsigned integer in base&nbsp;8.
 183      *
 184      * <p>The unsigned {@code long} value is the argument plus
 185      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 186      * equal to the argument.  This value is converted to a string of
 187      * ASCII digits in octal (base&nbsp;8) with no extra leading
 188      * {@code 0}s.
 189      *
 190      * <p>If the unsigned magnitude is zero, it is represented by a
 191      * single zero character {@code '0'}
 192      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 193      * the representation of the unsigned magnitude will not be the
 194      * zero character. The following characters are used as octal
 195      * digits:
 196      *
 197      * <blockquote>
 198      *  {@code 01234567}
 199      * </blockquote>
 200      *
 201      * These are the characters <code>'&#92;u0030'</code> through
 202      * <code>'&#92;u0037'</code>.
 203      *
 204      * @param   i   a {@code long} to be converted to a string.
 205      * @return  the string representation of the unsigned {@code long}
 206      *          value represented by the argument in octal (base&nbsp;8).
 207      * @since   JDK 1.0.2
 208      */
 209     public static String toOctalString(long i) {
 210         return toUnsignedString(i, 3);
 211     }
 212 
 213     /**
 214      * Returns a string representation of the {@code long}
 215      * argument as an unsigned integer in base&nbsp;2.
 216      *
 217      * <p>The unsigned {@code long} value is the argument plus
 218      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 219      * equal to the argument.  This value is converted to a string of
 220      * ASCII digits in binary (base&nbsp;2) with no extra leading
 221      * {@code 0}s.  If the unsigned magnitude is zero, it is
 222      * represented by a single zero character {@code '0'}
 223      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 224      * the representation of the unsigned magnitude will not be the
 225      * zero character. The characters {@code '0'}
 226      * (<code>'&#92;u0030'</code>) and {@code '1'}
 227      * (<code>'&#92;u0031'</code>) are used as binary digits.
 228      *
 229      * @param   i   a {@code long} to be converted to a string.
 230      * @return  the string representation of the unsigned {@code long}
 231      *          value represented by the argument in binary (base&nbsp;2).
 232      * @since   JDK 1.0.2
 233      */
 234     public static String toBinaryString(long i) {
 235         return toUnsignedString(i, 1);
 236     }
 237 
 238     /**
 239      * Convert the integer to an unsigned number.
 240      */
 241     private static String toUnsignedString(long i, int shift) {
 242         char[] buf = new char[64];
 243         int charPos = 64;
 244         int radix = 1 << shift;
 245         long mask = radix - 1;
 246         do {
 247             buf[--charPos] = Integer.digits[(int)(i & mask)];
 248             i >>>= shift;
 249         } while (i != 0);
 250         return new String(buf, charPos, (64 - charPos));
 251     }
 252 
 253     /**
 254      * Returns a {@code String} object representing the specified
 255      * {@code long}.  The argument is converted to signed decimal
 256      * representation and returned as a string, exactly as if the
 257      * argument and the radix 10 were given as arguments to the {@link
 258      * #toString(long, int)} method.
 259      *
 260      * @param   i   a {@code long} to be converted.
 261      * @return  a string representation of the argument in base&nbsp;10.
 262      */
 263     public static String toString(long i) {
 264         if (i == Long.MIN_VALUE)
 265             return "-9223372036854775808";
 266         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 267         char[] buf = new char[size];
 268         getChars(i, size, buf);
 269         return new String(0, size, buf);
 270     }
 271 
 272     /**

















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


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





























































































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


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

































































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




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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


 124         boolean negative = (i < 0);
 125 
 126         if (!negative) {
 127             i = -i;
 128         }
 129 
 130         while (i <= -radix) {
 131             buf[charPos--] = Integer.digits[(int)(-(i % radix))];
 132             i = i / radix;
 133         }
 134         buf[charPos] = Integer.digits[(int)(-i)];
 135 
 136         if (negative) {
 137             buf[--charPos] = '-';
 138         }
 139 
 140         return new String(buf, charPos, (65 - charPos));
 141     }
 142 
 143     /**
 144      * Returns an unsigned string representation of the first argument
 145      * in the radix specified by the second argument.
 146      *
 147      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
 148      * or larger than {@code Character.MAX_RADIX}, then the radix
 149      * {@code 10} is used instead.
 150      *
 151      * <p>Note that since the first argument is treated as an unsigned
 152      * value, no leading sign character is printed.
 153      *
 154      * <p>If the magnitude is zero, it is represented by a single zero
 155      * character {@code '0'} (<code>'&#92;u0030'</code>); otherwise,
 156      * the first character of the representation of the magnitude will
 157      * not be the zero character.
 158      *
 159      * <p>The characters used as digits and the behavior of radixes
 160      * is the same as {@link #toString(long, int) toString}.
 161      *
 162      * @param   i       an integer to be converted to an unsigned string.
 163      * @param   radix   the radix to use in the string representation.
 164      * @return  an unsigned string representation of the argument in the specified radix.
 165      * @see     #toString(long, int)
 166      * @since 1.7
 167      */
 168     public static String toUnsignedString(long i, int radix) {
 169         if (i >= 0)
 170             return toString(i, radix);
 171         else {
 172             return toUnsignedBigInteger(i).toString(radix);
 173         }
 174     }
 175 
 176     /**
 177      * Return a BigInteger equal to the unsigned value of the
 178      * argument.
 179      */
 180     private static BigInteger toUnsignedBigInteger(long i) {
 181         int upper = (int) ((i >> 32) & 0xffffffff);
 182         int lower = (int) i;
 183 
 184          // return (upper << 32) + lower
 185         return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).
 186             add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
 187     }
 188 
 189     /**
 190      * Returns a string representation of the {@code long}
 191      * argument as an unsigned integer in base&nbsp;16.
 192      *
 193      * <p>The unsigned {@code long} value is the argument plus
 194      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 195      * equal to the argument.  This value is converted to a string of
 196      * ASCII digits in hexadecimal (base&nbsp;16) with no extra
 197      * leading {@code 0}s.  If the unsigned magnitude is zero, it
 198      * is represented by a single zero character {@code '0'}
 199      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 200      * the representation of the unsigned magnitude will not be the
 201      * zero character. The following characters are used as
 202      * hexadecimal digits:
 203      *
 204      * <blockquote>
 205      *  {@code 0123456789abcdef}
 206      * </blockquote>
 207      *
 208      * These are the characters <code>'&#92;u0030'</code> through
 209      * <code>'&#92;u0039'</code> and  <code>'&#92;u0061'</code> through
 210      * <code>'&#92;u0066'</code>.  If uppercase letters are desired,
 211      * the {@link java.lang.String#toUpperCase()} method may be called
 212      * on the result:
 213      *
 214      * <blockquote>
 215      *  {@code Long.toHexString(n).toUpperCase()}
 216      * </blockquote>
 217      *
 218      * @param   i   a {@code long} to be converted to a string.
 219      * @return  the string representation of the unsigned {@code long}
 220      *          value represented by the argument in hexadecimal
 221      *          (base&nbsp;16).
 222      * @since   JDK 1.0.2
 223      */
 224     public static String toHexString(long i) {
 225         return toUnsignedString0(i, 4);
 226     }
 227 
 228     /**
 229      * Returns a string representation of the {@code long}
 230      * argument as an unsigned integer in base&nbsp;8.
 231      *
 232      * <p>The unsigned {@code long} value is the argument plus
 233      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 234      * equal to the argument.  This value is converted to a string of
 235      * ASCII digits in octal (base&nbsp;8) with no extra leading
 236      * {@code 0}s.
 237      *
 238      * <p>If the unsigned magnitude is zero, it is represented by a
 239      * single zero character {@code '0'}
 240      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 241      * the representation of the unsigned magnitude will not be the
 242      * zero character. The following characters are used as octal
 243      * digits:
 244      *
 245      * <blockquote>
 246      *  {@code 01234567}
 247      * </blockquote>
 248      *
 249      * These are the characters <code>'&#92;u0030'</code> through
 250      * <code>'&#92;u0037'</code>.
 251      *
 252      * @param   i   a {@code long} to be converted to a string.
 253      * @return  the string representation of the unsigned {@code long}
 254      *          value represented by the argument in octal (base&nbsp;8).
 255      * @since   JDK 1.0.2
 256      */
 257     public static String toOctalString(long i) {
 258         return toUnsignedString0(i, 3);
 259     }
 260 
 261     /**
 262      * Returns a string representation of the {@code long}
 263      * argument as an unsigned integer in base&nbsp;2.
 264      *
 265      * <p>The unsigned {@code long} value is the argument plus
 266      * 2<sup>64</sup> if the argument is negative; otherwise, it is
 267      * equal to the argument.  This value is converted to a string of
 268      * ASCII digits in binary (base&nbsp;2) with no extra leading
 269      * {@code 0}s.  If the unsigned magnitude is zero, it is
 270      * represented by a single zero character {@code '0'}
 271      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 272      * the representation of the unsigned magnitude will not be the
 273      * zero character. The characters {@code '0'}
 274      * (<code>'&#92;u0030'</code>) and {@code '1'}
 275      * (<code>'&#92;u0031'</code>) are used as binary digits.
 276      *
 277      * @param   i   a {@code long} to be converted to a string.
 278      * @return  the string representation of the unsigned {@code long}
 279      *          value represented by the argument in binary (base&nbsp;2).
 280      * @since   JDK 1.0.2
 281      */
 282     public static String toBinaryString(long i) {
 283         return toUnsignedString0(i, 1);
 284     }
 285 
 286     /**
 287      * Convert the integer to an unsigned number.
 288      */
 289     private static String toUnsignedString0(long i, int shift) {
 290         char[] buf = new char[64];
 291         int charPos = 64;
 292         int radix = 1 << shift;
 293         long mask = radix - 1;
 294         do {
 295             buf[--charPos] = Integer.digits[(int)(i & mask)];
 296             i >>>= shift;
 297         } while (i != 0);
 298         return new String(buf, charPos, (64 - charPos));
 299     }
 300 
 301     /**
 302      * Returns a {@code String} object representing the specified
 303      * {@code long}.  The argument is converted to signed decimal
 304      * representation and returned as a string, exactly as if the
 305      * argument and the radix 10 were given as arguments to the {@link
 306      * #toString(long, int)} method.
 307      *
 308      * @param   i   a {@code long} to be converted.
 309      * @return  a string representation of the argument in base&nbsp;10.
 310      */
 311     public static String toString(long i) {
 312         if (i == Long.MIN_VALUE)
 313             return "-9223372036854775808";
 314         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 315         char[] buf = new char[size];
 316         getChars(i, size, buf);
 317         return new String(0, size, buf);
 318     }
 319 
 320     /**
 321      * Returns an unsigned string representation of the argument.
 322      *
 323      * The argument is converted to unsigned decimal representation
 324      * and returned as a string exactly as if the argument and radix
 325      * 10 were given as arguments to the {@link #toUnsignedString(long,
 326      * int)} method.
 327      *
 328      * @param   i  an integer to be converted to an unsigned string.
 329      * @return  an unsigned string representation of the argument.
 330      * @see     #toUnsignedString(long, int)
 331      * @since 1.7
 332      */
 333     public static String toUnsignedString(long i) {
 334         return toUnsignedString(i, 10);
 335     }
 336 
 337     /**
 338      * Places characters representing the integer i into the
 339      * character array buf. The characters are placed into
 340      * the buffer backwards starting with the least significant
 341      * digit at the specified index (exclusive), and working
 342      * backwards from there.
 343      *
 344      * Will fail if i == Long.MIN_VALUE
 345      */
 346     static void getChars(long i, int index, char[] buf) {
 347         long q;
 348         int r;
 349         int charPos = index;
 350         char sign = 0;
 351 
 352         if (i < 0) {
 353             sign = '-';
 354             i = -i;
 355         }
 356 
 357         // Get 2 digits/iteration using longs until quotient fits into an int


 532      * #parseLong(java.lang.String, int)} method.
 533      *
 534      * <p>Note that neither the character {@code L}
 535      * (<code>'&#92;u004C'</code>) nor {@code l}
 536      * (<code>'&#92;u006C'</code>) is permitted to appear at the end
 537      * of the string as a type indicator, as would be permitted in
 538      * Java programming language source code.
 539      *
 540      * @param      s   a {@code String} containing the {@code long}
 541      *             representation to be parsed
 542      * @return     the {@code long} represented by the argument in
 543      *             decimal.
 544      * @throws     NumberFormatException  if the string does not contain a
 545      *             parsable {@code long}.
 546      */
 547     public static long parseLong(String s) throws NumberFormatException {
 548         return parseLong(s, 10);
 549     }
 550 
 551     /**
 552      * Parses the string argument as an unsigned integer in the radix
 553      * specified by the second argument.
 554      *
 555      * The characters in the string must all be digits of the
 556      * specified radix (as determined by whether {@link
 557      * java.lang.Character#digit(char, int)} returns a nonnegative
 558      * value), except that the first character may be an ASCII plus
 559      * sign {@code '+'} (<code>'&#92;u002B'</code>). The resulting
 560      * integer value is returned.
 561      *
 562      * <p>An exception of type {@code NumberFormatException} is
 563      * thrown if any of the following situations occurs:
 564      * <ul>
 565      * <li>The first argument is {@code null} or is a string of
 566      * length zero.
 567      *
 568      * <li>The radix is either smaller than
 569      * {@link java.lang.Character#MIN_RADIX} or
 570      * larger than {@link java.lang.Character#MAX_RADIX}.
 571      *
 572      * <li>Any character of the string is not a digit of the specified
 573      * radix, except that the first character may be a plus sign
 574      * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
 575      * string is longer than length 1.
 576      *
 577      * <li>The value represented by the string is larger than the
 578      * largest unsigned {@code long}.
 579      *
 580      * </ul>
 581      *
 582      *
 583      * @param      s   the {@code String} containing the unsigned integer
 584      *                  representation to be parsed
 585      * @param      radix   the radix to be used while parsing {@code s}.
 586      * @return     the integer represented by the string argument in the
 587      *             specified radix.
 588      * @throws     NumberFormatException if the {@code String}
 589      *             does not contain a parsable {@code long}.
 590      * @since 1.7
 591      */
 592     public static long parseUnsignedLong(String s, int radix)
 593                 throws NumberFormatException {
 594         if (s == null)  {
 595             throw new NumberFormatException("null");
 596         }
 597 
 598         int len = s.length();
 599         if (len > 0) {
 600             char firstChar = s.charAt(0);
 601             if (firstChar == '-') {
 602                 throw new 
 603                     NumberFormatException(String.format("Illegal leading minus sign " +
 604                                                        "on unsigned string %s.", s));
 605             } else {
 606                 BigInteger bi = new BigInteger(s, radix);
 607 
 608                 // Largest *unsigned* value is all ones in binary
 609                 BigInteger limit = toUnsignedBigInteger(-1L);
 610                 
 611                 if (bi.compareTo(limit) <= 0) {
 612                     return bi.longValue();
 613                 } else {
 614                     throw new
 615                     NumberFormatException(String.format("String value %s exceeds " + 
 616                                                         "range of unsigned long.", s));
 617                 }
 618             }
 619         } else {
 620             throw NumberFormatException.forInputString(s);
 621         }
 622     }
 623 
 624     /**
 625      * Parses the string argument as an unsigned decimal integer. The
 626      * characters in the string must all be decimal digits, except
 627      * that the first character may be an an ASCII plus sign {@code
 628      * '+'} (<code>'&#92;u002B'</code>). The resulting integer value
 629      * is returned, exactly as if the argument and the radix 10 were
 630      * given as arguments to the {@link
 631      * #parseUnsignedLong(java.lang.String, int)} method.
 632      *
 633      * @param s   a {@code String} containing the unsigned {@code long}
 634      *            representation to be parsed
 635      * @return    the unsigned integer value represented by the argument in decimal.
 636      * @throws    NumberFormatException  if the string does not contain a
 637      *            parsable unsigned integer.
 638      * @since 1.7
 639      */
 640     public static long parseUnsignedLong(String s) throws NumberFormatException {
 641         return parseUnsignedLong(s, 10);
 642     }
 643 
 644     /**
 645      * Returns a {@code Long} object holding the value
 646      * extracted from the specified {@code String} when parsed
 647      * with the radix given by the second argument.  The first
 648      * argument is interpreted as representing a signed
 649      * {@code long} in the radix specified by the second
 650      * argument, exactly as if the arguments were given to the {@link
 651      * #parseLong(java.lang.String, int)} method. The result is a
 652      * {@code Long} object that represents the {@code long}
 653      * value specified by the string.
 654      *
 655      * <p>In other words, this method returns a {@code Long} object equal
 656      * to the value of:
 657      *
 658      * <blockquote>
 659      *  {@code new Long(Long.parseLong(s, radix))}
 660      * </blockquote>
 661      *
 662      * @param      s       the string to be parsed
 663      * @param      radix   the radix to be used in interpreting {@code s}
 664      * @return     a {@code Long} object holding the value


1112     }
1113 
1114     /**
1115      * Compares two {@code long} values numerically.
1116      * The value returned is identical to what would be returned by:
1117      * <pre>
1118      *    Long.valueOf(x).compareTo(Long.valueOf(y))
1119      * </pre>
1120      *
1121      * @param  x the first {@code long} to compare
1122      * @param  y the second {@code long} to compare
1123      * @return the value {@code 0} if {@code x == y};
1124      *         a value less than {@code 0} if {@code x < y}; and
1125      *         a value greater than {@code 0} if {@code x > y}
1126      * @since 1.7
1127      */
1128     public static int compare(long x, long y) {
1129         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1130     }
1131 
1132     /**
1133      * Compares two {@code long} values numerically treating the values
1134      * as unsigned.
1135      *
1136      * @param  x the first {@code long} to compare
1137      * @param  y the second {@code long} to compare
1138      * @return the value {@code 0} if {@code x == y}; a value less
1139      *         than {@code 0} if {@code x < y} as unsigned values; and
1140      *         a value greater than {@code 0} if {@code x > y} as
1141      *         unsigned values
1142      * @since 1.7
1143      */
1144     public static int compareUnsigned(long x, long y) {
1145         long sign_x = x & Long.MIN_VALUE;
1146         long sign_y = y & Long.MIN_VALUE;
1147 
1148         if (sign_x == sign_y) {
1149             return Long.compare(x & (~Long.MIN_VALUE), y & (~Long.MIN_VALUE));
1150         }
1151         else {
1152             if (sign_x == 0L)
1153                 return -1; // sign(x) is 0, sign(y) is 1 => (x < y)
1154             else
1155                 return 1; //  sign(x) is 1, sign(y) is 0 => (x > y)
1156         }
1157     }
1158 
1159 
1160     /**
1161      * Returns the unsigned quotient of dividing the first argument by
1162      * the second where each argument is interpreted as an unsigned
1163      * value.
1164      *
1165      * In other words, return the unsigned value of {@code
1166      * (dividend / divisor)}.
1167      *
1168      * @return the unsigned quotient of the first argument divided by
1169      * the second argument
1170      * @param dividend the value to be divided
1171      * @param divisor the value doing the dividing
1172      * @since 1.7
1173      */
1174     public static long divideUnsigned(long dividend, long divisor) {
1175         return toUnsignedBigInteger(dividend).
1176             divide(toUnsignedBigInteger(divisor)).longValue();
1177     }
1178 
1179     /**
1180      * Returns the unsigned remainder from dividing the first argument by
1181      * the second where each argument is interpreted as an unsigned
1182      * value.
1183      *
1184      * In other words, return the unsigned value of {@code
1185      * (dividend % divisor)}.
1186      *
1187      * @return the unsigned remainder of the first argument divided by
1188      * the second argument
1189      * @param dividend the value to be divided
1190      * @param divisor the value doing the dividing
1191      * @since 1.7
1192      */
1193     public static long remainderUnsigned(long dividend, long divisor) {
1194         return toUnsignedBigInteger(dividend).
1195             remainder(toUnsignedBigInteger(divisor)).longValue();
1196     }
1197 
1198     // Bit Twiddling
1199 
1200     /**
1201      * The number of bits used to represent a {@code long} value in two's
1202      * complement binary form.
1203      *
1204      * @since 1.5
1205      */
1206     public static final int SIZE = 64;
1207 
1208     /**
1209      * Returns a {@code long} value with at most a single one-bit, in the
1210      * position of the highest-order ("leftmost") one-bit in the specified
1211      * {@code long} value.  Returns zero if the specified value has no
1212      * one-bits in its two's complement binary representation, that is, if it
1213      * is equal to zero.
1214      *
1215      * @return a {@code long} value with a single one-bit, in the position
1216      *     of the highest-order one-bit in the specified value, or zero if