src/share/classes/java/lang/Integer.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


 111      * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
 112      * <code>'&#92;u007A'</code>. If {@code radix} is
 113      * <var>N</var>, then the first <var>N</var> of these characters
 114      * are used as radix-<var>N</var> digits in the order shown. Thus,
 115      * the digits for hexadecimal (radix 16) are
 116      * {@code 0123456789abcdef}. If uppercase letters are
 117      * desired, the {@link java.lang.String#toUpperCase()} method may
 118      * be called on the result:
 119      *
 120      * <blockquote>
 121      *  {@code Integer.toString(n, 16).toUpperCase()}
 122      * </blockquote>
 123      *
 124      * @param   i       an integer to be converted to a string.
 125      * @param   radix   the radix to use in the string representation.
 126      * @return  a string representation of the argument in the specified radix.
 127      * @see     java.lang.Character#MAX_RADIX
 128      * @see     java.lang.Character#MIN_RADIX
 129      */
 130     public static String toString(int i, int radix) {
 131 
 132         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
 133             radix = 10;
 134 
 135         /* Use the faster version */
 136         if (radix == 10) {
 137             return toString(i);
 138         }
 139 
 140         char buf[] = new char[33];
 141         boolean negative = (i < 0);
 142         int charPos = 32;
 143 
 144         if (!negative) {
 145             i = -i;
 146         }
 147 
 148         while (i <= -radix) {
 149             buf[charPos--] = digits[-(i % radix)];
 150             i = i / radix;
 151         }
 152         buf[charPos] = digits[-i];
 153 
 154         if (negative) {
 155             buf[--charPos] = '-';
 156         }
 157 
 158         return new String(buf, charPos, (33 - charPos));
 159     }
 160 
 161     /**





























 162      * Returns a string representation of the integer argument as an
 163      * unsigned integer in base&nbsp;16.
 164      *
 165      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 166      * if the argument is negative; otherwise, it is equal to the
 167      * argument.  This value is converted to a string of ASCII digits
 168      * in hexadecimal (base&nbsp;16) with no extra leading
 169      * {@code 0}s. If the unsigned magnitude is zero, it is
 170      * represented by a single zero character {@code '0'}
 171      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 172      * the representation of the unsigned magnitude will not be the
 173      * zero character. The following characters are used as
 174      * hexadecimal digits:






 175      *
 176      * <blockquote>
 177      *  {@code 0123456789abcdef}
 178      * </blockquote>
 179      *
 180      * These are the characters <code>'&#92;u0030'</code> through
 181      * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
 182      * <code>'&#92;u0066'</code>. If uppercase letters are
 183      * desired, the {@link java.lang.String#toUpperCase()} method may
 184      * be called on the result:
 185      *
 186      * <blockquote>
 187      *  {@code Integer.toHexString(n).toUpperCase()}
 188      * </blockquote>
 189      *
 190      * @param   i   an integer to be converted to a string.
 191      * @return  the string representation of the unsigned integer value
 192      *          represented by the argument in hexadecimal (base&nbsp;16).


 193      * @since   JDK1.0.2
 194      */
 195     public static String toHexString(int i) {
 196         return toUnsignedString(i, 4);
 197     }
 198 
 199     /**
 200      * Returns a string representation of the integer argument as an
 201      * unsigned integer in base&nbsp;8.
 202      *
 203      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 204      * if the argument is negative; otherwise, it is equal to the
 205      * argument.  This value is converted to a string of ASCII digits
 206      * in octal (base&nbsp;8) with no extra leading {@code 0}s.
 207      *





 208      * <p>If the unsigned magnitude is zero, it is represented by a
 209      * single zero character {@code '0'}
 210      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 211      * the representation of the unsigned magnitude will not be the
 212      * zero character. The following characters are used as octal
 213      * digits:
 214      *
 215      * <blockquote>
 216      * {@code 01234567}
 217      * </blockquote>
 218      *
 219      * These are the characters <code>'&#92;u0030'</code> through
 220      * <code>'&#92;u0037'</code>.
 221      *
 222      * @param   i   an integer to be converted to a string.
 223      * @return  the string representation of the unsigned integer value
 224      *          represented by the argument in octal (base&nbsp;8).


 225      * @since   JDK1.0.2
 226      */
 227     public static String toOctalString(int i) {
 228         return toUnsignedString(i, 3);
 229     }
 230 
 231     /**
 232      * Returns a string representation of the integer argument as an
 233      * unsigned integer in base&nbsp;2.
 234      *
 235      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 236      * if the argument is negative; otherwise it is equal to the
 237      * argument.  This value is converted to a string of ASCII digits
 238      * in binary (base&nbsp;2) with no extra leading {@code 0}s.
 239      * If the unsigned magnitude is zero, it is represented by a
 240      * single zero character {@code '0'}
 241      * (<code>'&#92;u0030'</code>); otherwise, the first character of
 242      * the representation of the unsigned magnitude will not be the
 243      * zero character. The characters {@code '0'}
 244      * (<code>'&#92;u0030'</code>) and {@code '1'}
 245      * (<code>'&#92;u0031'</code>) are used as binary digits.





 246      *
 247      * @param   i   an integer to be converted to a string.
 248      * @return  the string representation of the unsigned integer value
 249      *          represented by the argument in binary (base&nbsp;2).


 250      * @since   JDK1.0.2
 251      */
 252     public static String toBinaryString(int i) {
 253         return toUnsignedString(i, 1);
 254     }
 255 
 256     /**
 257      * Convert the integer to an unsigned number.
 258      */
 259     private static String toUnsignedString(int i, int shift) {
 260         char[] buf = new char[32];
 261         int charPos = 32;
 262         int radix = 1 << shift;
 263         int mask = radix - 1;
 264         do {
 265             buf[--charPos] = digits[i & mask];
 266             i >>>= shift;
 267         } while (i != 0);
 268 
 269         return new String(buf, charPos, (32 - charPos));
 270     }
 271 
 272 
 273     final static char [] DigitTens = {
 274         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 275         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 276         '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 277         '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 278         '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 279         '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',


 318     /**
 319      * Returns a {@code String} object representing the
 320      * specified integer. The argument is converted to signed decimal
 321      * representation and returned as a string, exactly as if the
 322      * argument and radix 10 were given as arguments to the {@link
 323      * #toString(int, int)} method.
 324      *
 325      * @param   i   an integer to be converted.
 326      * @return  a string representation of the argument in base&nbsp;10.
 327      */
 328     public static String toString(int i) {
 329         if (i == Integer.MIN_VALUE)
 330             return "-2147483648";
 331         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 332         char[] buf = new char[size];
 333         getChars(i, size, buf);
 334         return new String(0, size, buf);
 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 == Integer.MIN_VALUE
 345      */
 346     static void getChars(int i, int index, char[] buf) {
 347         int q, r;
 348         int charPos = index;
 349         char sign = 0;
 350 
 351         if (i < 0) {
 352             sign = '-';
 353             i = -i;
 354         }
 355 
 356         // Generate two digits per iteration
 357         while (i >= 65536) {


 512      * characters in the string must all be decimal digits, except
 513      * that the first character may be an ASCII minus sign {@code '-'}
 514      * (<code>'&#92;u002D'</code>) to indicate a negative value or an
 515      * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
 516      * indicate a positive value. The resulting integer value is
 517      * returned, exactly as if the argument and the radix 10 were
 518      * given as arguments to the {@link #parseInt(java.lang.String,
 519      * int)} method.
 520      *
 521      * @param s    a {@code String} containing the {@code int}
 522      *             representation to be parsed
 523      * @return     the integer value represented by the argument in decimal.
 524      * @exception  NumberFormatException  if the string does not contain a
 525      *               parsable integer.
 526      */
 527     public static int parseInt(String s) throws NumberFormatException {
 528         return parseInt(s,10);
 529     }
 530 
 531     /**






























































































 532      * Returns an {@code Integer} object holding the value
 533      * extracted from the specified {@code String} when parsed
 534      * with the radix given by the second argument. The first argument
 535      * is interpreted as representing a signed integer in the radix
 536      * specified by the second argument, exactly as if the arguments
 537      * were given to the {@link #parseInt(java.lang.String, int)}
 538      * method. The result is an {@code Integer} object that
 539      * represents the integer value specified by the string.
 540      *
 541      * <p>In other words, this method returns an {@code Integer}
 542      * object equal to the value of:
 543      *
 544      * <blockquote>
 545      *  {@code new Integer(Integer.parseInt(s, radix))}
 546      * </blockquote>
 547      *
 548      * @param      s   the string to be parsed.
 549      * @param      radix the radix to be used in interpreting {@code s}
 550      * @return     an {@code Integer} object holding the value
 551      *             represented by the string argument in the specified


1013     }
1014 
1015     /**
1016      * Compares two {@code int} values numerically.
1017      * The value returned is identical to what would be returned by:
1018      * <pre>
1019      *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
1020      * </pre>
1021      *
1022      * @param  x the first {@code int} to compare
1023      * @param  y the second {@code int} to compare
1024      * @return the value {@code 0} if {@code x == y};
1025      *         a value less than {@code 0} if {@code x < y}; and
1026      *         a value greater than {@code 0} if {@code x > y}
1027      * @since 1.7
1028      */
1029     public static int compare(int x, int y) {
1030         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1031     }
1032 






































































1033 
1034     // Bit twiddling
1035 
1036     /**
1037      * The number of bits used to represent an {@code int} value in two's
1038      * complement binary form.
1039      *
1040      * @since 1.5
1041      */
1042     public static final int SIZE = 32;
1043 
1044     /**
1045      * Returns an {@code int} value with at most a single one-bit, in the
1046      * position of the highest-order ("leftmost") one-bit in the specified
1047      * {@code int} value.  Returns zero if the specified value has no
1048      * one-bits in its two's complement binary representation, that is, if it
1049      * is equal to zero.
1050      *
1051      * @return an {@code int} value with a single one-bit, in the position
1052      *     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


 111      * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
 112      * <code>'&#92;u007A'</code>. If {@code radix} is
 113      * <var>N</var>, then the first <var>N</var> of these characters
 114      * are used as radix-<var>N</var> digits in the order shown. Thus,
 115      * the digits for hexadecimal (radix 16) are
 116      * {@code 0123456789abcdef}. If uppercase letters are
 117      * desired, the {@link java.lang.String#toUpperCase()} method may
 118      * be called on the result:
 119      *
 120      * <blockquote>
 121      *  {@code Integer.toString(n, 16).toUpperCase()}
 122      * </blockquote>
 123      *
 124      * @param   i       an integer to be converted to a string.
 125      * @param   radix   the radix to use in the string representation.
 126      * @return  a string representation of the argument in the specified radix.
 127      * @see     java.lang.Character#MAX_RADIX
 128      * @see     java.lang.Character#MIN_RADIX
 129      */
 130     public static String toString(int i, int radix) {

 131         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
 132             radix = 10;
 133 
 134         /* Use the faster version */
 135         if (radix == 10) {
 136             return toString(i);
 137         }
 138 
 139         char buf[] = new char[33];
 140         boolean negative = (i < 0);
 141         int charPos = 32;
 142 
 143         if (!negative) {
 144             i = -i;
 145         }
 146 
 147         while (i <= -radix) {
 148             buf[charPos--] = digits[-(i % radix)];
 149             i = i / radix;
 150         }
 151         buf[charPos] = digits[-i];
 152 
 153         if (negative) {
 154             buf[--charPos] = '-';
 155         }
 156 
 157         return new String(buf, charPos, (33 - charPos));
 158     }
 159 
 160     /**
 161      * Returns an unsigned string representation of the first argument
 162      * in the radix specified by the second argument.
 163      *
 164      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
 165      * or larger than {@code Character.MAX_RADIX}, then the radix
 166      * {@code 10} is used instead.
 167      *
 168      * <p>Note that since the first argument is treated as an unsigned
 169      * value, no leading sign character is printed.
 170      *
 171      * <p>If the magnitude is zero, it is represented by a single zero
 172      * character {@code '0'} (<code>'&#92;u0030'</code>); otherwise,
 173      * the first character of the representation of the magnitude will
 174      * not be the zero character.
 175      *
 176      * <p>The characters used as digits and the behavior of radixes
 177      * is the same as {@link #toString(int, int) toString}.
 178      *
 179      * @param   i       an integer to be converted to an unsigned string.
 180      * @param   radix   the radix to use in the string representation.
 181      * @return  an unsigned string representation of the argument in the specified radix.
 182      * @see     #toString(int, int)
 183      * @since 1.8
 184      */
 185     public static String toUnsignedString(int i, int radix) {
 186         return Long.toString(toUnsignedLong(i), radix);
 187     }
 188 
 189     /**
 190      * Returns a string representation of the integer argument as an
 191      * unsigned integer in base&nbsp;16.
 192      *
 193      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 194      * if the argument is negative; otherwise, it is equal to the
 195      * argument.  This value is converted to a string of ASCII digits
 196      * in hexadecimal (base&nbsp;16) with no extra leading
 197      * {@code 0}s.
 198      *
 199      * <p>The value of the argument can be recovered from the returned
 200      * string {@code s} by calling {@link
 201      * Integer#parseUnsignedInt(String, int)
 202      * Integer.parseUnsignedInt(s, 16)}.
 203      *
 204      * <p>If the unsigned magnitude is zero, it is represented by a
 205      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 206      * otherwise, the first character of the representation of the
 207      * unsigned magnitude will not be the zero character. The
 208      * following characters are used as hexadecimal digits:
 209      *
 210      * <blockquote>
 211      *  {@code 0123456789abcdef}
 212      * </blockquote>
 213      *
 214      * These are the characters <code>'&#92;u0030'</code> through
 215      * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
 216      * <code>'&#92;u0066'</code>. If uppercase letters are
 217      * desired, the {@link java.lang.String#toUpperCase()} method may
 218      * be called on the result:
 219      *
 220      * <blockquote>
 221      *  {@code Integer.toHexString(n).toUpperCase()}
 222      * </blockquote>
 223      *
 224      * @param   i   an integer to be converted to a string.
 225      * @return  the string representation of the unsigned integer value
 226      *          represented by the argument in hexadecimal (base&nbsp;16).
 227      * @see #parseUnsignedInt(String, int)
 228      * @see #toUnsignedString(int, int)
 229      * @since   JDK1.0.2
 230      */
 231     public static String toHexString(int i) {
 232         return toUnsignedString0(i, 4);
 233     }
 234 
 235     /**
 236      * Returns a string representation of the integer argument as an
 237      * unsigned integer in base&nbsp;8.
 238      *
 239      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 240      * if the argument is negative; otherwise, it is equal to the
 241      * argument.  This value is converted to a string of ASCII digits
 242      * in octal (base&nbsp;8) with no extra leading {@code 0}s.
 243      *
 244      * <p>The value of the argument can be recovered from the returned
 245      * string {@code s} by calling {@link
 246      * Integer#parseUnsignedInt(String, int)
 247      * Integer.parseUnsignedInt(s, 8)}.
 248      *
 249      * <p>If the unsigned magnitude is zero, it is represented by a
 250      * single zero character {@code '0'} (<code>'&#92;u0030'</code>);
 251      * otherwise, the first character of the representation of the
 252      * unsigned magnitude will not be the zero character. The
 253      * following characters are used as octal digits:

 254      *
 255      * <blockquote>
 256      * {@code 01234567}
 257      * </blockquote>
 258      *
 259      * These are the characters <code>'&#92;u0030'</code> through
 260      * <code>'&#92;u0037'</code>.
 261      *
 262      * @param   i   an integer to be converted to a string.
 263      * @return  the string representation of the unsigned integer value
 264      *          represented by the argument in octal (base&nbsp;8).
 265      * @see #parseUnsignedInt(String, int)
 266      * @see #toUnsignedString(int, int)
 267      * @since   JDK1.0.2
 268      */
 269     public static String toOctalString(int i) {
 270         return toUnsignedString0(i, 3);
 271     }
 272 
 273     /**
 274      * Returns a string representation of the integer argument as an
 275      * unsigned integer in base&nbsp;2.
 276      *
 277      * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
 278      * if the argument is negative; otherwise it is equal to the
 279      * argument.  This value is converted to a string of ASCII digits
 280      * in binary (base&nbsp;2) with no extra leading {@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      * Integer#parseUnsignedInt(String, int)
 285      * Integer.parseUnsignedInt(s, 2)}.
 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      * characters {@code '0'} (<code>'&#92;u0030'</code>) and {@code
 292      * '1'} (<code>'&#92;u0031'</code>) are used as binary digits.
 293      *
 294      * @param   i   an integer to be converted to a string.
 295      * @return  the string representation of the unsigned integer value
 296      *          represented by the argument in binary (base&nbsp;2).
 297      * @see #parseUnsignedInt(String, int)
 298      * @see #toUnsignedString(int, int)
 299      * @since   JDK1.0.2
 300      */
 301     public static String toBinaryString(int i) {
 302         return toUnsignedString0(i, 1);
 303     }
 304 
 305     /**
 306      * Convert the integer to an unsigned number.
 307      */
 308     private static String toUnsignedString0(int i, int shift) {
 309         char[] buf = new char[32];
 310         int charPos = 32;
 311         int radix = 1 << shift;
 312         int mask = radix - 1;
 313         do {
 314             buf[--charPos] = digits[i & mask];
 315             i >>>= shift;
 316         } while (i != 0);
 317 
 318         return new String(buf, charPos, (32 - charPos));
 319     }
 320 
 321 
 322     final static char [] DigitTens = {
 323         '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 324         '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 325         '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 326         '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 327         '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 328         '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',


 367     /**
 368      * Returns a {@code String} object representing the
 369      * specified integer. The argument is converted to signed decimal
 370      * representation and returned as a string, exactly as if the
 371      * argument and radix 10 were given as arguments to the {@link
 372      * #toString(int, int)} method.
 373      *
 374      * @param   i   an integer to be converted.
 375      * @return  a string representation of the argument in base&nbsp;10.
 376      */
 377     public static String toString(int i) {
 378         if (i == Integer.MIN_VALUE)
 379             return "-2147483648";
 380         int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
 381         char[] buf = new char[size];
 382         getChars(i, size, buf);
 383         return new String(0, size, buf);
 384     }
 385 
 386     /**
 387      * Returns an unsigned string representation of the argument.
 388      *
 389      * The argument is converted to unsigned decimal representation
 390      * and returned as a string exactly as if the argument and radix
 391      * 10 were given as arguments to the {@link #toUnsignedString(int,
 392      * int)} method.
 393      *
 394      * @param   i  an integer to be converted to an unsigned string.
 395      * @return  an unsigned string representation of the argument.
 396      * @see     #toUnsignedString(int, int)
 397      * @since 1.8
 398      */
 399     public static String toUnsignedString(int i) {
 400         return Long.toString(toUnsignedLong(i));
 401     }
 402 
 403     /**
 404      * Places characters representing the integer i into the
 405      * character array buf. The characters are placed into
 406      * the buffer backwards starting with the least significant
 407      * digit at the specified index (exclusive), and working
 408      * backwards from there.
 409      *
 410      * Will fail if i == Integer.MIN_VALUE
 411      */
 412     static void getChars(int i, int index, char[] buf) {
 413         int q, r;
 414         int charPos = index;
 415         char sign = 0;
 416 
 417         if (i < 0) {
 418             sign = '-';
 419             i = -i;
 420         }
 421 
 422         // Generate two digits per iteration
 423         while (i >= 65536) {


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


1173     }
1174 
1175     /**
1176      * Compares two {@code int} values numerically.
1177      * The value returned is identical to what would be returned by:
1178      * <pre>
1179      *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
1180      * </pre>
1181      *
1182      * @param  x the first {@code int} to compare
1183      * @param  y the second {@code int} to compare
1184      * @return the value {@code 0} if {@code x == y};
1185      *         a value less than {@code 0} if {@code x < y}; and
1186      *         a value greater than {@code 0} if {@code x > y}
1187      * @since 1.7
1188      */
1189     public static int compare(int x, int y) {
1190         return (x < y) ? -1 : ((x == y) ? 0 : 1);
1191     }
1192 
1193     /**
1194      * Compares two {@code int} values numerically treating the values
1195      * as unsigned.
1196      *
1197      * @param  x the first {@code int} to compare
1198      * @param  y the second {@code int} to compare
1199      * @return the value {@code 0} if {@code x == y}; a value less
1200      *         than {@code 0} if {@code x < y} as unsigned values; and
1201      *         a value greater than {@code 0} if {@code x > y} as
1202      *         unsigned values
1203      * @since 1.8
1204      */
1205     public static int compareUnsigned(int x, int y) {
1206         return compare(x + MIN_VALUE, y + MIN_VALUE);
1207     }
1208     
1209     /**
1210      * Converts the argument to a {@code long} by an unsigned
1211      * conversion.  In an unsigned conversion to a {@code long}, the
1212      * high-order 32 bits of the {@code long} are zero and the
1213      * low-order 32 bits are equal to the bits of the integer
1214      * argument.
1215      *
1216      * @return the argument converted to {@code long} by an unsigned
1217      *         conversion
1218      * @param  x the value to convert to an unsigned {@code long}
1219      * @since 1.8
1220      */
1221     public static long toUnsignedLong(int x) {
1222         return ((long) x) & 0xffffffffL;
1223     }
1224 
1225     /**
1226      * Returns the unsigned quotient of dividing the first argument by
1227      * the second where each argument is interpreted as an unsigned
1228      * value.
1229      *
1230      * In other words, return the unsigned value of {@code
1231      * (dividend / divisor)}.
1232      *
1233      * @return the unsigned quotient of the first argument divided by
1234      * the second argument
1235      * @param dividend the value to be divided
1236      * @param divisor the value doing the dividing
1237      * @since 1.8
1238      */
1239     public static int divideUnsigned(int dividend, int divisor) {
1240         // In lieu of tricky code, for now just use long arithmetic.
1241         return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
1242     }
1243 
1244     /**
1245      * Returns the unsigned remainder from dividing the first argument by
1246      * the second where each argument is interpreted as an unsigned
1247      * value.
1248      *
1249      * In other words, return the unsigned value of {@code
1250      * (dividend % divisor)}.
1251      *
1252      * @return the unsigned remainder of the first argument divided by
1253      * the second argument
1254      * @param dividend the value to be divided
1255      * @param divisor the value doing the dividing
1256      * @since 1.8
1257      */
1258     public static int remainderUnsigned(int dividend, int divisor) {
1259         // In lieu of tricky code, for now just use long arithmetic.
1260         return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
1261     }
1262 
1263     
1264     // Bit twiddling
1265 
1266     /**
1267      * The number of bits used to represent an {@code int} value in two's
1268      * complement binary form.
1269      *
1270      * @since 1.5
1271      */
1272     public static final int SIZE = 32;
1273 
1274     /**
1275      * Returns an {@code int} value with at most a single one-bit, in the
1276      * position of the highest-order ("leftmost") one-bit in the specified
1277      * {@code int} 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 an {@code int} value with a single one-bit, in the position
1282      *     of the highest-order one-bit in the specified value, or zero if