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>'\u0039'</code> and <code>'\u0061'</code> through
112 * <code>'\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 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 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>'\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>'\u0030'</code> through
181 * <code>'\u0039'</code> and <code>'\u0061'</code> through
182 * <code>'\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 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 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 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>'\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>'\u0030'</code> through
220 * <code>'\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 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 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 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>'\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>'\u0030'</code>) and {@code '1'}
245 * (<code>'\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 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 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>'\u002D'</code>) to indicate a negative value or an
515 * ASCII plus sign {@code '+'} (<code>'\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>'\u0039'</code> and <code>'\u0061'</code> through
112 * <code>'\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 integer value in the radix specified by the second
163 * argument.
164 *
165 * <p>If the radix is smaller than {@code Character.MIN_RADIX}
166 * or larger than {@code Character.MAX_RADIX}, then the radix
167 * {@code 10} is used instead.
168 *
169 * <p>Note that since the first argument is treated as an unsigned
170 * value, no leading sign character is printed.
171 *
172 * <p>If the magnitude is zero, it is represented by a single zero
173 * character {@code '0'} (<code>'\u0030'</code>); otherwise,
174 * the first character of the representation of the magnitude will
175 * not be the zero character.
176 *
177 * <p>The behavior of radixes and the characters used as digits
178 * are the same as {@link #toString(int, int) toString}.
179 *
180 * @param i an integer to be converted to an unsigned string.
181 * @param radix the radix to use in the string representation.
182 * @return an unsigned string representation of the argument in the specified radix.
183 * @see #toString(int, int)
184 * @since 1.8
185 */
186 public static String toUnsignedString(int i, int radix) {
187 return Long.toString(toUnsignedLong(i), radix);
188 }
189
190 /**
191 * Returns a string representation of the integer argument as an
192 * unsigned integer in base 16.
193 *
194 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
195 * if the argument is negative; otherwise, it is equal to the
196 * argument. This value is converted to a string of ASCII digits
197 * in hexadecimal (base 16) with no extra leading
198 * {@code 0}s.
199 *
200 * <p>The value of the argument can be recovered from the returned
201 * string {@code s} by calling {@link
202 * Integer#parseUnsignedInt(String, int)
203 * Integer.parseUnsignedInt(s, 16)}.
204 *
205 * <p>If the unsigned magnitude is zero, it is represented by a
206 * single zero character {@code '0'} (<code>'\u0030'</code>);
207 * otherwise, the first character of the representation of the
208 * unsigned magnitude will not be the zero character. The
209 * following characters are used as hexadecimal digits:
210 *
211 * <blockquote>
212 * {@code 0123456789abcdef}
213 * </blockquote>
214 *
215 * These are the characters <code>'\u0030'</code> through
216 * <code>'\u0039'</code> and <code>'\u0061'</code> through
217 * <code>'\u0066'</code>. If uppercase letters are
218 * desired, the {@link java.lang.String#toUpperCase()} method may
219 * be called on the result:
220 *
221 * <blockquote>
222 * {@code Integer.toHexString(n).toUpperCase()}
223 * </blockquote>
224 *
225 * @param i an integer to be converted to a string.
226 * @return the string representation of the unsigned integer value
227 * represented by the argument in hexadecimal (base 16).
228 * @see #parseUnsignedInt(String, int)
229 * @see #toUnsignedString(int, int)
230 * @since JDK1.0.2
231 */
232 public static String toHexString(int i) {
233 return toUnsignedString0(i, 4);
234 }
235
236 /**
237 * Returns a string representation of the integer argument as an
238 * unsigned integer in base 8.
239 *
240 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
241 * if the argument is negative; otherwise, it is equal to the
242 * argument. This value is converted to a string of ASCII digits
243 * in octal (base 8) with no extra leading {@code 0}s.
244 *
245 * <p>The value of the argument can be recovered from the returned
246 * string {@code s} by calling {@link
247 * Integer#parseUnsignedInt(String, int)
248 * Integer.parseUnsignedInt(s, 8)}.
249 *
250 * <p>If the unsigned magnitude is zero, it is represented by a
251 * single zero character {@code '0'} (<code>'\u0030'</code>);
252 * otherwise, the first character of the representation of the
253 * unsigned magnitude will not be the zero character. The
254 * following characters are used as octal digits:
255 *
256 * <blockquote>
257 * {@code 01234567}
258 * </blockquote>
259 *
260 * These are the characters <code>'\u0030'</code> through
261 * <code>'\u0037'</code>.
262 *
263 * @param i an integer to be converted to a string.
264 * @return the string representation of the unsigned integer value
265 * represented by the argument in octal (base 8).
266 * @see #parseUnsignedInt(String, int)
267 * @see #toUnsignedString(int, int)
268 * @since JDK1.0.2
269 */
270 public static String toOctalString(int i) {
271 return toUnsignedString0(i, 3);
272 }
273
274 /**
275 * Returns a string representation of the integer argument as an
276 * unsigned integer in base 2.
277 *
278 * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
279 * if the argument is negative; otherwise it is equal to the
280 * argument. This value is converted to a string of ASCII digits
281 * in binary (base 2) with no extra leading {@code 0}s.
282 *
283 * <p>The value of the argument can be recovered from the returned
284 * string {@code s} by calling {@link
285 * Integer#parseUnsignedInt(String, int)
286 * Integer.parseUnsignedInt(s, 2)}.
287 *
288 * <p>If the unsigned magnitude is zero, it is represented by a
289 * single zero character {@code '0'} (<code>'\u0030'</code>);
290 * otherwise, the first character of the representation of the
291 * unsigned magnitude will not be the zero character. The
292 * characters {@code '0'} (<code>'\u0030'</code>) and {@code
293 * '1'} (<code>'\u0031'</code>) are used as binary digits.
294 *
295 * @param i an integer to be converted to a string.
296 * @return the string representation of the unsigned integer value
297 * represented by the argument in binary (base 2).
298 * @see #parseUnsignedInt(String, int)
299 * @see #toUnsignedString(int, int)
300 * @since JDK1.0.2
301 */
302 public static String toBinaryString(int i) {
303 return toUnsignedString0(i, 1);
304 }
305
306 /**
307 * Convert the integer to an unsigned number.
308 */
309 private static String toUnsignedString0(int i, int shift) {
310 char[] buf = new char[32];
311 int charPos = 32;
312 int radix = 1 << shift;
313 int mask = radix - 1;
314 do {
315 buf[--charPos] = digits[i & mask];
316 i >>>= shift;
317 } while (i != 0);
318
319 return new String(buf, charPos, (32 - charPos));
320 }
321
322
323 final static char [] DigitTens = {
324 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
325 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
326 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
327 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
328 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
329 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
368 /**
369 * Returns a {@code String} object representing the
370 * specified integer. The argument is converted to signed decimal
371 * representation and returned as a string, exactly as if the
372 * argument and radix 10 were given as arguments to the {@link
373 * #toString(int, int)} method.
374 *
375 * @param i an integer to be converted.
376 * @return a string representation of the argument in base 10.
377 */
378 public static String toString(int i) {
379 if (i == Integer.MIN_VALUE)
380 return "-2147483648";
381 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
382 char[] buf = new char[size];
383 getChars(i, size, buf);
384 return new String(0, size, buf);
385 }
386
387 /**
388 * Returns a string representation of the argument as an unsigned
389 * decimal value.
390 *
391 * The argument is converted to unsigned decimal representation
392 * and returned as a string exactly as if the argument and radix
393 * 10 were given as arguments to the {@link #toUnsignedString(int,
394 * int)} method.
395 *
396 * @param i an integer to be converted to an unsigned string.
397 * @return an unsigned string representation of the argument.
398 * @see #toUnsignedString(int, int)
399 * @since 1.8
400 */
401 public static String toUnsignedString(int i) {
402 return Long.toString(toUnsignedLong(i));
403 }
404
405 /**
406 * Places characters representing the integer i into the
407 * character array buf. The characters are placed into
408 * the buffer backwards starting with the least significant
409 * digit at the specified index (exclusive), and working
410 * backwards from there.
411 *
412 * Will fail if i == Integer.MIN_VALUE
413 */
414 static void getChars(int i, int index, char[] buf) {
415 int q, r;
416 int charPos = index;
417 char sign = 0;
418
419 if (i < 0) {
420 sign = '-';
421 i = -i;
422 }
423
424 // Generate two digits per iteration
425 while (i >= 65536) {
580 * characters in the string must all be decimal digits, except
581 * that the first character may be an ASCII minus sign {@code '-'}
582 * (<code>'\u002D'</code>) to indicate a negative value or an
583 * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
584 * indicate a positive value. The resulting integer value is
585 * returned, exactly as if the argument and the radix 10 were
586 * given as arguments to the {@link #parseInt(java.lang.String,
587 * int)} method.
588 *
589 * @param s a {@code String} containing the {@code int}
590 * representation to be parsed
591 * @return the integer value represented by the argument in decimal.
592 * @exception NumberFormatException if the string does not contain a
593 * parsable integer.
594 */
595 public static int parseInt(String s) throws NumberFormatException {
596 return parseInt(s,10);
597 }
598
599 /**
600 * Parses the string argument as an unsigned integer in the radix
601 * specified by the second argument. An unsigned integer maps the
602 * values usually associated with negative numbers to positive
603 * numbers larger than {@code MAX_VALUE}.
604 *
605 * The characters in the string must all be digits of the
606 * specified radix (as determined by whether {@link
607 * java.lang.Character#digit(char, int)} returns a nonnegative
608 * value), except that the first character may be an ASCII plus
609 * sign {@code '+'} (<code>'\u002B'</code>). The resulting
610 * integer value is returned.
611 *
612 * <p>An exception of type {@code NumberFormatException} is
613 * thrown if any of the following situations occurs:
614 * <ul>
615 * <li>The first argument is {@code null} or is a string of
616 * length zero.
617 *
618 * <li>The radix is either smaller than
619 * {@link java.lang.Character#MIN_RADIX} or
620 * larger than {@link java.lang.Character#MAX_RADIX}.
621 *
622 * <li>Any character of the string is not a digit of the specified
623 * radix, except that the first character may be a plus sign
624 * {@code '+'} (<code>'\u002B'</code>) provided that the
625 * string is longer than length 1.
626 *
627 * <li>The value represented by the string is larger than the
628 * largest unsigned {@code int}, 2<sup>32</sup>-1.
629 *
630 * </ul>
631 *
632 *
633 * @param s the {@code String} containing the unsigned integer
634 * representation to be parsed
635 * @param radix the radix to be used while parsing {@code s}.
636 * @return the integer represented by the string argument in the
637 * specified radix.
638 * @throws NumberFormatException if the {@code String}
639 * does not contain a parsable {@code int}.
640 * @since 1.8
641 */
642 public static int parseUnsignedInt(String s, int radix)
643 throws NumberFormatException {
644 if (s == null) {
645 throw new NumberFormatException("null");
646 }
647
648 int len = s.length();
649 if (len > 0) {
650 char firstChar = s.charAt(0);
651 if (firstChar == '-') {
652 throw new
653 NumberFormatException(String.format("Illegal leading minus sign " +
654 "on unsigned string %s.", s));
655 } else {
656 if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
657 (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
658 return parseInt(s, radix);
659 } else {
660 long ell = Long.parseLong(s, radix);
661 if ((ell & 0xffff_ffff_0000_0000L) == 0) {
662 return (int) ell;
663 } else {
664 throw new
665 NumberFormatException(String.format("String value %s exceeds " +
666 "range of unsigned int.", s));
667 }
668 }
669 }
670 } else {
671 throw NumberFormatException.forInputString(s);
672 }
673 }
674
675 /**
676 * Parses the string argument as an unsigned decimal integer. The
677 * characters in the string must all be decimal digits, except
678 * that the first character may be an an ASCII plus sign {@code
679 * '+'} (<code>'\u002B'</code>). The resulting integer value
680 * is returned, exactly as if the argument and the radix 10 were
681 * given as arguments to the {@link
682 * #parseUnsignedInt(java.lang.String, int)} method.
683 *
684 * @param s a {@code String} containing the unsigned {@code int}
685 * representation to be parsed
686 * @return the unsigned integer value represented by the argument in decimal.
687 * @throws NumberFormatException if the string does not contain a
688 * parsable unsigned integer.
689 * @since 1.8
690 */
691 public static int parseUnsignedInt(String s) throws NumberFormatException {
692 return parseUnsignedInt(s, 10);
693 }
694
695 /**
696 * Returns an {@code Integer} object holding the value
697 * extracted from the specified {@code String} when parsed
698 * with the radix given by the second argument. The first argument
699 * is interpreted as representing a signed integer in the radix
700 * specified by the second argument, exactly as if the arguments
701 * were given to the {@link #parseInt(java.lang.String, int)}
702 * method. The result is an {@code Integer} object that
703 * represents the integer value specified by the string.
704 *
705 * <p>In other words, this method returns an {@code Integer}
706 * object equal to the value of:
707 *
708 * <blockquote>
709 * {@code new Integer(Integer.parseInt(s, radix))}
710 * </blockquote>
711 *
712 * @param s the string to be parsed.
713 * @param radix the radix to be used in interpreting {@code s}
714 * @return an {@code Integer} object holding the value
715 * represented by the string argument in the specified
1175 public int compareTo(Integer anotherInteger) {
1176 return compare(this.value, anotherInteger.value);
1177 }
1178
1179 /**
1180 * Compares two {@code int} values numerically.
1181 * The value returned is identical to what would be returned by:
1182 * <pre>
1183 * Integer.valueOf(x).compareTo(Integer.valueOf(y))
1184 * </pre>
1185 *
1186 * @param x the first {@code int} to compare
1187 * @param y the second {@code int} to compare
1188 * @return the value {@code 0} if {@code x == y};
1189 * a value less than {@code 0} if {@code x < y}; and
1190 * a value greater than {@code 0} if {@code x > y}
1191 * @since 1.7
1192 */
1193 public static int compare(int x, int y) {
1194 return (x < y) ? -1 : ((x == y) ? 0 : 1);
1195 }
1196
1197 /**
1198 * Compares two {@code int} values numerically treating the values
1199 * as unsigned.
1200 *
1201 * @param x the first {@code int} to compare
1202 * @param y the second {@code int} to compare
1203 * @return the value {@code 0} if {@code x == y}; a value less
1204 * than {@code 0} if {@code x < y} as unsigned values; and
1205 * a value greater than {@code 0} if {@code x > y} as
1206 * unsigned values
1207 * @since 1.8
1208 */
1209 public static int compareUnsigned(int x, int y) {
1210 return compare(x + MIN_VALUE, y + MIN_VALUE);
1211 }
1212
1213 /**
1214 * Converts the argument to a {@code long} by an unsigned
1215 * conversion. In an unsigned conversion to a {@code long}, the
1216 * high-order 32 bits of the {@code long} are zero and the
1217 * low-order 32 bits are equal to the bits of the integer
1218 * argument.
1219 *
1220 * Consequently, zero and positive {@code int} values are mapped
1221 * to a numerically equal {@code long} value and negative {@code
1222 * int} values are mapped to a {@code long} value equal to the
1223 * input plus 2<sup>32</sup>.
1224 *
1225 * @param x the value to convert to an unsigned {@code long}
1226 * @return the argument converted to {@code long} by an unsigned
1227 * conversion
1228 * @since 1.8
1229 */
1230 public static long toUnsignedLong(int x) {
1231 return ((long) x) & 0xffffffffL;
1232 }
1233
1234 /**
1235 * Returns the unsigned quotient of dividing the first argument by
1236 * the second where each argument and the result is interpreted as
1237 * an unsigned value.
1238 *
1239 * <p>Note that in two's complement arithmetic, the three other
1240 * basic arithmetic operations of add, subtract, and multiply are
1241 * bit-wise identical if the two operands are regarded as both
1242 * being signed or both being unsigned. Therefore separate {@code
1243 * addUnsigned}, etc. methods are not provided.
1244 *
1245 * @param dividend the value to be divided
1246 * @param divisor the value doing the dividing
1247 * @return the unsigned quotient of the first argument divided by
1248 * the second argument
1249 * @see #remainderUnsigned
1250 * @since 1.8
1251 */
1252 public static int divideUnsigned(int dividend, int divisor) {
1253 // In lieu of tricky code, for now just use long arithmetic.
1254 return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
1255 }
1256
1257 /**
1258 * Returns the unsigned remainder from dividing the first argument
1259 * by the second where each argument and the result is interpreted
1260 * as an unsigned value.
1261 *
1262 * @param dividend the value to be divided
1263 * @param divisor the value doing the dividing
1264 * @return the unsigned remainder of the first argument divided by
1265 * the second argument
1266 * @see #divideUnsigned
1267 * @since 1.8
1268 */
1269 public static int remainderUnsigned(int dividend, int divisor) {
1270 // In lieu of tricky code, for now just use long arithmetic.
1271 return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
1272 }
1273
1274
1275 // Bit twiddling
1276
1277 /**
1278 * The number of bits used to represent an {@code int} value in two's
1279 * complement binary form.
1280 *
1281 * @since 1.5
1282 */
1283 public static final int SIZE = 32;
1284
1285 /**
1286 * Returns an {@code int} value with at most a single one-bit, in the
1287 * position of the highest-order ("leftmost") one-bit in the specified
1288 * {@code int} value. Returns zero if the specified value has no
1289 * one-bits in its two's complement binary representation, that is, if it
1290 * is equal to zero.
1291 *
|