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


1011     public int compareTo(Integer anotherInteger) {
1012         return compare(this.value, anotherInteger.value);
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      *


   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 a string representation of the first argument as an
 162      * unsigned string 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 behavior of radixes and the characters used as digits
 177      * are 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 a string representation of the argument as an unsigned
 388      * string.
 389      *
 390      * The argument is converted to unsigned decimal representation
 391      * and returned as a string exactly as if the argument and radix
 392      * 10 were given as arguments to the {@link #toUnsignedString(int,
 393      * int)} method.
 394      *
 395      * @param   i  an integer to be converted to an unsigned string.
 396      * @return  an unsigned string representation of the argument.
 397      * @see     #toUnsignedString(int, int)
 398      * @since 1.8
 399      */
 400     public static String toUnsignedString(int i) {
 401         return Long.toString(toUnsignedLong(i));
 402     }
 403 
 404     /**
 405      * Places characters representing the integer i into the
 406      * character array buf. The characters are placed into
 407      * the buffer backwards starting with the least significant
 408      * digit at the specified index (exclusive), and working
 409      * backwards from there.
 410      *
 411      * Will fail if i == Integer.MIN_VALUE
 412      */
 413     static void getChars(int i, int index, char[] buf) {
 414         int q, r;
 415         int charPos = index;
 416         char sign = 0;
 417 
 418         if (i < 0) {
 419             sign = '-';
 420             i = -i;
 421         }
 422 
 423         // Generate two digits per iteration
 424         while (i >= 65536) {


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


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