Source Home >> Java Source 1.6.0 >> java.lang.Integer V 0.09
  • 0001/*
  • 0002 * @(#)Integer.java 1.92 06/04/07
  • 0003 *
  • 0004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 0005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 0006 */
  • 0007
  • 0008package java.lang;
  • 0009
  • 0010/**
  • 0011 * The <code>Integer</code> class wraps a value of the primitive type
  • 0012 * <code>int</code> in an object. An object of type
  • 0013 * <code>Integer</code> contains a single field whose type is
  • 0014 * <code>int</code>.
  • 0015 *
  • 0016 * <p>
  • 0017 *
  • 0018 * In addition, this class provides several methods for converting an
  • 0019 * <code>int</code> to a <code>String</code> and a <code>String</code>
  • 0020 * to an <code>int</code>, as well as other constants and methods
  • 0021 * useful when dealing with an <code>int</code>.
  • 0022 *
  • 0023 * <p>Implementation note: The implementations of the "bit twiddling"
  • 0024 * methods (such as {@link #highestOneBit(int) highestOneBit} and
  • 0025 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  • 0026 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  • 0027 * Delight</i>, (Addison Wesley, 2002).
  • 0028 *
  • 0029 * @author Lee Boynton
  • 0030 * @author Arthur van Hoff
  • 0031 * @author Josh Bloch
  • 0032 * @version 1.92, 04/07/06
  • 0033 * @since JDK1.0
  • 0034 */
  • 0035public final class Integer extends Number implements Comparable<Integer> {
  • 0036 /**
  • 0037 * A constant holding the minimum value an <code>int</code> can
  • 0038 * have, -2<sup>31</sup>.
  • 0039 */
  • 0040 public static final int MIN_VALUE = 0x80000000;
  • 0041
  • 0042 /**
  • 0043 * A constant holding the maximum value an <code>int</code> can
  • 0044 * have, 2<sup>31</sup>-1.
  • 0045 */
  • 0046 public static final int MAX_VALUE = 0x7fffffff;
  • 0047
  • 0048 /**
  • 0049 * The <code>Class</code> instance representing the primitive type
  • 0050 * <code>int</code>.
  • 0051 *
  • 0052 * @since JDK1.1
  • 0053 */
  • 0054 public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
  • 0055
  • 0056 /**
  • 0057 * All possible chars for representing a number as a String
  • 0058 */
  • 0059 final static char[] digits = {
  • 0060 '0' , '1' , '2' , '3' , '4' , '5' ,
  • 0061 '6' , '7' , '8' , '9' , 'a' , 'b' ,
  • 0062 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  • 0063 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  • 0064 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  • 0065 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
  • 0066 };
  • 0067
  • 0068 /**
  • 0069 * Returns a string representation of the first argument in the
  • 0070 * radix specified by the second argument.
  • 0071 * <p>
  • 0072 * If the radix is smaller than <code>Character.MIN_RADIX</code>
  • 0073 * or larger than <code>Character.MAX_RADIX</code>, then the radix
  • 0074 * <code>10</code> is used instead.
  • 0075 * <p>
  • 0076 * If the first argument is negative, the first element of the
  • 0077 * result is the ASCII minus character <code>'-'</code>
  • 0078 * (<code>'\u002D'</code>). If the first argument is not
  • 0079 * negative, no sign character appears in the result.
  • 0080 * <p>
  • 0081 * The remaining characters of the result represent the magnitude
  • 0082 * of the first argument. If the magnitude is zero, it is
  • 0083 * represented by a single zero character <code>'0'</code>
  • 0084 * (<code>'\u0030'</code>); otherwise, the first character of
  • 0085 * the representation of the magnitude will not be the zero
  • 0086 * character. The following ASCII characters are used as digits:
  • 0087 * <blockquote><pre>
  • 0088 * 0123456789abcdefghijklmnopqrstuvwxyz
  • 0089 * </pre></blockquote>
  • 0090 * These are <code>'\u0030'</code> through
  • 0091 * <code>'\u0039'</code> and <code>'\u0061'</code> through
  • 0092 * <code>'\u007A'</code>. If <code>radix</code> is
  • 0093 * <var>N</var>, then the first <var>N</var> of these characters
  • 0094 * are used as radix-<var>N</var> digits in the order shown. Thus,
  • 0095 * the digits for hexadecimal (radix 16) are
  • 0096 * <code>0123456789abcdef</code>. If uppercase letters are
  • 0097 * desired, the {@link java.lang.String#toUpperCase()} method may
  • 0098 * be called on the result:
  • 0099 * <blockquote><pre>
  • 0100 * Integer.toString(n, 16).toUpperCase()
  • 0101 * </pre></blockquote>
  • 0102 *
  • 0103 * @param i an integer to be converted to a string.
  • 0104 * @param radix the radix to use in the string representation.
  • 0105 * @return a string representation of the argument in the specified radix.
  • 0106 * @see java.lang.Character#MAX_RADIX
  • 0107 * @see java.lang.Character#MIN_RADIX
  • 0108 */
  • 0109 public static String toString(int i, int radix) {
  • 0110
  • 0111 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  • 0112 radix = 10;
  • 0113
  • 0114 /* Use the faster version */
  • 0115 if (radix == 10) {
  • 0116 return toString(i);
  • 0117 }
  • 0118
  • 0119 char buf[] = new char[33];
  • 0120 boolean negative = (i < 0);
  • 0121 int charPos = 32;
  • 0122
  • 0123 if (!negative) {
  • 0124 i = -i;
  • 0125 }
  • 0126
  • 0127 while (i <= -radix) {
  • 0128 buf[charPos--] = digits[-(i % radix)];
  • 0129 i = i / radix;
  • 0130 }
  • 0131 buf[charPos] = digits[-i];
  • 0132
  • 0133 if (negative) {
  • 0134 buf[--charPos] = '-';
  • 0135 }
  • 0136
  • 0137 return new String(buf, charPos, (33 - charPos));
  • 0138 }
  • 0139
  • 0140 /**
  • 0141 * Returns a string representation of the integer argument as an
  • 0142 * unsigned integer in base 16.
  • 0143 * <p>
  • 0144 * The unsigned integer value is the argument plus 2<sup>32</sup>
  • 0145 * if the argument is negative; otherwise, it is equal to the
  • 0146 * argument. This value is converted to a string of ASCII digits
  • 0147 * in hexadecimal (base 16) with no extra leading
  • 0148 * <code>0</code>s. If the unsigned magnitude is zero, it is
  • 0149 * represented by a single zero character <code>'0'</code>
  • 0150 * (<code>'\u0030'</code>); otherwise, the first character of
  • 0151 * the representation of the unsigned magnitude will not be the
  • 0152 * zero character. The following characters are used as
  • 0153 * hexadecimal digits:
  • 0154 * <blockquote><pre>
  • 0155 * 0123456789abcdef
  • 0156 * </pre></blockquote>
  • 0157 * These are the characters <code>'\u0030'</code> through
  • 0158 * <code>'\u0039'</code> and <code>'\u0061'</code> through
  • 0159 * <code>'\u0066'</code>. If uppercase letters are
  • 0160 * desired, the {@link java.lang.String#toUpperCase()} method may
  • 0161 * be called on the result:
  • 0162 * <blockquote><pre>
  • 0163 * Integer.toHexString(n).toUpperCase()
  • 0164 * </pre></blockquote>
  • 0165 *
  • 0166 * @param i an integer to be converted to a string.
  • 0167 * @return the string representation of the unsigned integer value
  • 0168 * represented by the argument in hexadecimal (base 16).
  • 0169 * @since JDK1.0.2
  • 0170 */
  • 0171 public static String toHexString(int i) {
  • 0172 return toUnsignedString(i, 4);
  • 0173 }
  • 0174
  • 0175 /**
  • 0176 * Returns a string representation of the integer argument as an
  • 0177 * unsigned integer in base 8.
  • 0178 * <p>
  • 0179 * The unsigned integer value is the argument plus 2<sup>32</sup>
  • 0180 * if the argument is negative; otherwise, it is equal to the
  • 0181 * argument. This value is converted to a string of ASCII digits
  • 0182 * in octal (base 8) with no extra leading <code>0</code>s.
  • 0183 * <p>
  • 0184 * If the unsigned magnitude is zero, it is represented by a
  • 0185 * single zero character <code>'0'</code>
  • 0186 * (<code>'\u0030'</code>); otherwise, the first character of
  • 0187 * the representation of the unsigned magnitude will not be the
  • 0188 * zero character. The following characters are used as octal
  • 0189 * digits:
  • 0190 * <blockquote><pre>
  • 0191 * 01234567
  • 0192 * </pre></blockquote>
  • 0193 * These are the characters <code>'\u0030'</code> through
  • 0194 * <code>'\u0037'</code>.
  • 0195 *
  • 0196 * @param i an integer to be converted to a string.
  • 0197 * @return the string representation of the unsigned integer value
  • 0198 * represented by the argument in octal (base 8).
  • 0199 * @since JDK1.0.2
  • 0200 */
  • 0201 public static String toOctalString(int i) {
  • 0202 return toUnsignedString(i, 3);
  • 0203 }
  • 0204
  • 0205 /**
  • 0206 * Returns a string representation of the integer argument as an
  • 0207 * unsigned integer in base 2.
  • 0208 * <p>
  • 0209 * The unsigned integer value is the argument plus 2<sup>32</sup>
  • 0210 * if the argument is negative; otherwise it is equal to the
  • 0211 * argument. This value is converted to a string of ASCII digits
  • 0212 * in binary (base 2) with no extra leading <code>0</code>s.
  • 0213 * If the unsigned magnitude is zero, it is represented by a
  • 0214 * single zero character <code>'0'</code>
  • 0215 * (<code>'\u0030'</code>); otherwise, the first character of
  • 0216 * the representation of the unsigned magnitude will not be the
  • 0217 * zero character. The characters <code>'0'</code>
  • 0218 * (<code>'\u0030'</code>) and <code>'1'</code>
  • 0219 * (<code>'\u0031'</code>) are used as binary digits.
  • 0220 *
  • 0221 * @param i an integer to be converted to a string.
  • 0222 * @return the string representation of the unsigned integer value
  • 0223 * represented by the argument in binary (base 2).
  • 0224 * @since JDK1.0.2
  • 0225 */
  • 0226 public static String toBinaryString(int i) {
  • 0227 return toUnsignedString(i, 1);
  • 0228 }
  • 0229
  • 0230 /**
  • 0231 * Convert the integer to an unsigned number.
  • 0232 */
  • 0233 private static String toUnsignedString(int i, int shift) {
  • 0234 char[] buf = new char[32];
  • 0235 int charPos = 32;
  • 0236 int radix = 1 << shift;
  • 0237 int mask = radix - 1;
  • 0238 do {
  • 0239 buf[--charPos] = digits[i & mask];
  • 0240 i >>>= shift;
  • 0241 } while (i != 0);
  • 0242
  • 0243 return new String(buf, charPos, (32 - charPos));
  • 0244 }
  • 0245
  • 0246
  • 0247 final static char [] DigitTens = {
  • 0248 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
  • 0249 '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
  • 0250 '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
  • 0251 '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
  • 0252 '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
  • 0253 '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
  • 0254 '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
  • 0255 '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
  • 0256 '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
  • 0257 '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
  • 0258 } ;
  • 0259
  • 0260 final static char [] DigitOnes = {
  • 0261 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0262 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0263 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0264 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0265 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0266 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0267 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0268 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0269 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0270 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  • 0271 } ;
  • 0272
  • 0273 // I use the "invariant division by multiplication" trick to
  • 0274 // accelerate Integer.toString. In particular we want to
  • 0275 // avoid division by 10.
  • 0276 //
  • 0277 // The "trick" has roughly the same performance characteristics
  • 0278 // as the "classic" Integer.toString code on a non-JIT VM.
  • 0279 // The trick avoids .rem and .div calls but has a longer code
  • 0280 // path and is thus dominated by dispatch overhead. In the
  • 0281 // JIT case the dispatch overhead doesn't exist and the
  • 0282 // "trick" is considerably faster than the classic code.
  • 0283 //
  • 0284 // TODO-FIXME: convert (x * 52429) into the equiv shift-add
  • 0285 // sequence.
  • 0286 //
  • 0287 // RE: Division by Invariant Integers using Multiplication
  • 0288 // T Gralund, P Montgomery
  • 0289 // ACM PLDI 1994
  • 0290 //
  • 0291
  • 0292 /**
  • 0293 * Returns a <code>String</code> object representing the
  • 0294 * specified integer. The argument is converted to signed decimal
  • 0295 * representation and returned as a string, exactly as if the
  • 0296 * argument and radix 10 were given as arguments to the {@link
  • 0297 * #toString(int, int)} method.
  • 0298 *
  • 0299 * @param i an integer to be converted.
  • 0300 * @return a string representation of the argument in base 10.
  • 0301 */
  • 0302 public static String toString(int i) {
  • 0303 if (i == Integer.MIN_VALUE)
  • 0304 return "-2147483648";
  • 0305 int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
  • 0306 char[] buf = new char[size];
  • 0307 getChars(i, size, buf);
  • 0308 return new String(0, size, buf);
  • 0309 }
  • 0310
  • 0311 /**
  • 0312 * Places characters representing the integer i into the
  • 0313 * character array buf. The characters are placed into
  • 0314 * the buffer backwards starting with the least significant
  • 0315 * digit at the specified index (exclusive), and working
  • 0316 * backwards from there.
  • 0317 *
  • 0318 * Will fail if i == Integer.MIN_VALUE
  • 0319 */
  • 0320 static void getChars(int i, int index, char[] buf) {
  • 0321 int q, r;
  • 0322 int charPos = index;
  • 0323 char sign = 0;
  • 0324
  • 0325 if (i < 0) {
  • 0326 sign = '-';
  • 0327 i = -i;
  • 0328 }
  • 0329
  • 0330 // Generate two digits per iteration
  • 0331 while (i >= 65536) {
  • 0332 q = i / 100;
  • 0333 // really: r = i - (q * 100);
  • 0334 r = i - ((q << 6) + (q << 5) + (q << 2));
  • 0335 i = q;
  • 0336 buf [--charPos] = DigitOnes[r];
  • 0337 buf [--charPos] = DigitTens[r];
  • 0338 }
  • 0339
  • 0340 // Fall thru to fast mode for smaller numbers
  • 0341 // assert(i <= 65536, i);
  • 0342 for (;;) {
  • 0343 q = (i * 52429) >>> (16+3);
  • 0344 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
  • 0345 buf [--charPos] = digits [r];
  • 0346 i = q;
  • 0347 if (i == 0) break;
  • 0348 }
  • 0349 if (sign != 0) {
  • 0350 buf [--charPos] = sign;
  • 0351 }
  • 0352 }
  • 0353
  • 0354 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
  • 0355 99999999, 999999999, Integer.MAX_VALUE };
  • 0356
  • 0357 // Requires positive x
  • 0358 static int stringSize(int x) {
  • 0359 for (int i=0; ; i++)
  • 0360 if (x <= sizeTable[i])
  • 0361 return i+1;
  • 0362 }
  • 0363
  • 0364 /**
  • 0365 * Parses the string argument as a signed integer in the radix
  • 0366 * specified by the second argument. The characters in the string
  • 0367 * must all be digits of the specified radix (as determined by
  • 0368 * whether {@link java.lang.Character#digit(char, int)} returns a
  • 0369 * nonnegative value), except that the first character may be an
  • 0370 * ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to
  • 0371 * indicate a negative value. The resulting integer value is returned.
  • 0372 * <p>
  • 0373 * An exception of type <code>NumberFormatException</code> is
  • 0374 * thrown if any of the following situations occurs:
  • 0375 * <ul>
  • 0376 * <li>The first argument is <code>null</code> or is a string of
  • 0377 * length zero.
  • 0378 * <li>The radix is either smaller than
  • 0379 * {@link java.lang.Character#MIN_RADIX} or
  • 0380 * larger than {@link java.lang.Character#MAX_RADIX}.
  • 0381 * <li>Any character of the string is not a digit of the specified
  • 0382 * radix, except that the first character may be a minus sign
  • 0383 * <code>'-'</code> (<code>'\u002D'</code>) provided that the
  • 0384 * string is longer than length 1.
  • 0385 * <li>The value represented by the string is not a value of type
  • 0386 * <code>int</code>.
  • 0387 * </ul><p>
  • 0388 * Examples:
  • 0389 * <blockquote><pre>
  • 0390 * parseInt("0", 10) returns 0
  • 0391 * parseInt("473", 10) returns 473
  • 0392 * parseInt("-0", 10) returns 0
  • 0393 * parseInt("-FF", 16) returns -255
  • 0394 * parseInt("1100110", 2) returns 102
  • 0395 * parseInt("2147483647", 10) returns 2147483647
  • 0396 * parseInt("-2147483648", 10) returns -2147483648
  • 0397 * parseInt("2147483648", 10) throws a NumberFormatException
  • 0398 * parseInt("99", 8) throws a NumberFormatException
  • 0399 * parseInt("Kona", 10) throws a NumberFormatException
  • 0400 * parseInt("Kona", 27) returns 411787
  • 0401 * </pre></blockquote>
  • 0402 *
  • 0403 * @param s the <code>String</code> containing the integer
  • 0404 * representation to be parsed
  • 0405 * @param radix the radix to be used while parsing <code>s</code>.
  • 0406 * @return the integer represented by the string argument in the
  • 0407 * specified radix.
  • 0408 * @exception NumberFormatException if the <code>String</code>
  • 0409 * does not contain a parsable <code>int</code>.
  • 0410 */
  • 0411 public static int parseInt(String s, int radix)
  • 0412 throws NumberFormatException
  • 0413 {
  • 0414 if (s == null) {
  • 0415 throw new NumberFormatException("null");
  • 0416 }
  • 0417
  • 0418 if (radix < Character.MIN_RADIX) {
  • 0419 throw new NumberFormatException("radix " + radix +
  • 0420 " less than Character.MIN_RADIX");
  • 0421 }
  • 0422
  • 0423 if (radix > Character.MAX_RADIX) {
  • 0424 throw new NumberFormatException("radix " + radix +
  • 0425 " greater than Character.MAX_RADIX");
  • 0426 }
  • 0427
  • 0428 int result = 0;
  • 0429 boolean negative = false;
  • 0430 int i = 0, max = s.length();
  • 0431 int limit;
  • 0432 int multmin;
  • 0433 int digit;
  • 0434
  • 0435 if (max > 0) {
  • 0436 if (s.charAt(0) == '-') {
  • 0437 negative = true;
  • 0438 limit = Integer.MIN_VALUE;
  • 0439 i++;
  • 0440 } else {
  • 0441 limit = -Integer.MAX_VALUE;
  • 0442 }
  • 0443 multmin = limit / radix;
  • 0444 if (i < max) {
  • 0445 digit = Character.digit(s.charAt(i++),radix);
  • 0446 if (digit < 0) {
  • 0447 throw NumberFormatException.forInputString(s);
  • 0448 } else {
  • 0449 result = -digit;
  • 0450 }
  • 0451 }
  • 0452 while (i < max) {
  • 0453 // Accumulating negatively avoids surprises near MAX_VALUE
  • 0454 digit = Character.digit(s.charAt(i++),radix);
  • 0455 if (digit < 0) {
  • 0456 throw NumberFormatException.forInputString(s);
  • 0457 }
  • 0458 if (result < multmin) {
  • 0459 throw NumberFormatException.forInputString(s);
  • 0460 }
  • 0461 result *= radix;
  • 0462 if (result < limit + digit) {
  • 0463 throw NumberFormatException.forInputString(s);
  • 0464 }
  • 0465 result -= digit;
  • 0466 }
  • 0467 } else {
  • 0468 throw NumberFormatException.forInputString(s);
  • 0469 }
  • 0470 if (negative) {
  • 0471 if (i > 1) {
  • 0472 return result;
  • 0473 } else { /* Only got "-" */
  • 0474 throw NumberFormatException.forInputString(s);
  • 0475 }
  • 0476 } else {
  • 0477 return -result;
  • 0478 }
  • 0479 }
  • 0480
  • 0481 /**
  • 0482 * Parses the string argument as a signed decimal integer. The
  • 0483 * characters in the string must all be decimal digits, except that
  • 0484 * the first character may be an ASCII minus sign <code>'-'</code>
  • 0485 * (<code>'\u002D'</code>) to indicate a negative value. The resulting
  • 0486 * integer value is returned, exactly as if the argument and the radix
  • 0487 * 10 were given as arguments to the
  • 0488 * {@link #parseInt(java.lang.String, int)} method.
  • 0489 *
  • 0490 * @param s a <code>String</code> containing the <code>int</code>
  • 0491 * representation to be parsed
  • 0492 * @return the integer value represented by the argument in decimal.
  • 0493 * @exception NumberFormatException if the string does not contain a
  • 0494 * parsable integer.
  • 0495 */
  • 0496 public static int parseInt(String s) throws NumberFormatException {
  • 0497 return parseInt(s,10);
  • 0498 }
  • 0499
  • 0500 /**
  • 0501 * Returns an <code>Integer</code> object holding the value
  • 0502 * extracted from the specified <code>String</code> when parsed
  • 0503 * with the radix given by the second argument. The first argument
  • 0504 * is interpreted as representing a signed integer in the radix
  • 0505 * specified by the second argument, exactly as if the arguments
  • 0506 * were given to the {@link #parseInt(java.lang.String, int)}
  • 0507 * method. The result is an <code>Integer</code> object that
  • 0508 * represents the integer value specified by the string.
  • 0509 * <p>
  • 0510 * In other words, this method returns an <code>Integer</code>
  • 0511 * object equal to the value of:
  • 0512 *
  • 0513 * <blockquote><code>
  • 0514 * new Integer(Integer.parseInt(s, radix))
  • 0515 * </code></blockquote>
  • 0516 *
  • 0517 * @param s the string to be parsed.
  • 0518 * @param radix the radix to be used in interpreting <code>s</code>
  • 0519 * @return an <code>Integer</code> object holding the value
  • 0520 * represented by the string argument in the specified
  • 0521 * radix.
  • 0522 * @exception NumberFormatException if the <code>String</code>
  • 0523 * does not contain a parsable <code>int</code>.
  • 0524 */
  • 0525 public static Integer valueOf(String s, int radix) throws NumberFormatException {
  • 0526 return new Integer(parseInt(s,radix));
  • 0527 }
  • 0528
  • 0529 /**
  • 0530 * Returns an <code>Integer</code> object holding the
  • 0531 * value of the specified <code>String</code>. The argument is
  • 0532 * interpreted as representing a signed decimal integer, exactly
  • 0533 * as if the argument were given to the {@link
  • 0534 * #parseInt(java.lang.String)} method. The result is an
  • 0535 * <code>Integer</code> object that represents the integer value
  • 0536 * specified by the string.
  • 0537 * <p>
  • 0538 * In other words, this method returns an <code>Integer</code>
  • 0539 * object equal to the value of:
  • 0540 *
  • 0541 * <blockquote><code>
  • 0542 * new Integer(Integer.parseInt(s))
  • 0543 * </code></blockquote>
  • 0544 *
  • 0545 * @param s the string to be parsed.
  • 0546 * @return an <code>Integer</code> object holding the value
  • 0547 * represented by the string argument.
  • 0548 * @exception NumberFormatException if the string cannot be parsed
  • 0549 * as an integer.
  • 0550 */
  • 0551 public static Integer valueOf(String s) throws NumberFormatException
  • 0552 {
  • 0553 return new Integer(parseInt(s, 10));
  • 0554 }
  • 0555
  • 0556 private static class IntegerCache {
  • 0557 private IntegerCache(){}
  • 0558
  • 0559 static final Integer cache[] = new Integer[-(-128) + 127 + 1];
  • 0560
  • 0561 static {
  • 0562 for(int i = 0; i < cache.length; i++)
  • 0563 cache[i] = new Integer(i - 128);
  • 0564 }
  • 0565 }
  • 0566
  • 0567 /**
  • 0568 * Returns a <tt>Integer</tt> instance representing the specified
  • 0569 * <tt>int</tt> value.
  • 0570 * If a new <tt>Integer</tt> instance is not required, this method
  • 0571 * should generally be used in preference to the constructor
  • 0572 * {@link #Integer(int)}, as this method is likely to yield
  • 0573 * significantly better space and time performance by caching
  • 0574 * frequently requested values.
  • 0575 *
  • 0576 * @param i an <code>int</code> value.
  • 0577 * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
  • 0578 * @since 1.5
  • 0579 */
  • 0580 public static Integer valueOf(int i) {
  • 0581 final int offset = 128;
  • 0582 if (i >= -128 && i <= 127) { // must cache
  • 0583 return IntegerCache.cache[i + offset];
  • 0584 }
  • 0585 return new Integer(i);
  • 0586 }
  • 0587
  • 0588 /**
  • 0589 * The value of the <code>Integer</code>.
  • 0590 *
  • 0591 * @serial
  • 0592 */
  • 0593 private final int value;
  • 0594
  • 0595 /**
  • 0596 * Constructs a newly allocated <code>Integer</code> object that
  • 0597 * represents the specified <code>int</code> value.
  • 0598 *
  • 0599 * @param value the value to be represented by the
  • 0600 * <code>Integer</code> object.
  • 0601 */
  • 0602 public Integer(int value) {
  • 0603 this.value = value;
  • 0604 }
  • 0605
  • 0606 /**
  • 0607 * Constructs a newly allocated <code>Integer</code> object that
  • 0608 * represents the <code>int</code> value indicated by the
  • 0609 * <code>String</code> parameter. The string is converted to an
  • 0610 * <code>int</code> value in exactly the manner used by the
  • 0611 * <code>parseInt</code> method for radix 10.
  • 0612 *
  • 0613 * @param s the <code>String</code> to be converted to an
  • 0614 * <code>Integer</code>.
  • 0615 * @exception NumberFormatException if the <code>String</code> does not
  • 0616 * contain a parsable integer.
  • 0617 * @see java.lang.Integer#parseInt(java.lang.String, int)
  • 0618 */
  • 0619 public Integer(String s) throws NumberFormatException {
  • 0620 this.value = parseInt(s, 10);
  • 0621 }
  • 0622
  • 0623 /**
  • 0624 * Returns the value of this <code>Integer</code> as a
  • 0625 * <code>byte</code>.
  • 0626 */
  • 0627 public byte byteValue() {
  • 0628 return (byte)value;
  • 0629 }
  • 0630
  • 0631 /**
  • 0632 * Returns the value of this <code>Integer</code> as a
  • 0633 * <code>short</code>.
  • 0634 */
  • 0635 public short shortValue() {
  • 0636 return (short)value;
  • 0637 }
  • 0638
  • 0639 /**
  • 0640 * Returns the value of this <code>Integer</code> as an
  • 0641 * <code>int</code>.
  • 0642 */
  • 0643 public int intValue() {
  • 0644 return value;
  • 0645 }
  • 0646
  • 0647 /**
  • 0648 * Returns the value of this <code>Integer</code> as a
  • 0649 * <code>long</code>.
  • 0650 */
  • 0651 public long longValue() {
  • 0652 return (long)value;
  • 0653 }
  • 0654
  • 0655 /**
  • 0656 * Returns the value of this <code>Integer</code> as a
  • 0657 * <code>float</code>.
  • 0658 */
  • 0659 public float floatValue() {
  • 0660 return (float)value;
  • 0661 }
  • 0662
  • 0663 /**
  • 0664 * Returns the value of this <code>Integer</code> as a
  • 0665 * <code>double</code>.
  • 0666 */
  • 0667 public double doubleValue() {
  • 0668 return (double)value;
  • 0669 }
  • 0670
  • 0671 /**
  • 0672 * Returns a <code>String</code> object representing this
  • 0673 * <code>Integer</code>'s value. The value is converted to signed
  • 0674 * decimal representation and returned as a string, exactly as if
  • 0675 * the integer value were given as an argument to the {@link
  • 0676 * java.lang.Integer#toString(int)} method.
  • 0677 *
  • 0678 * @return a string representation of the value of this object in
  • 0679 * base 10.
  • 0680 */
  • 0681 public String toString() {
  • 0682 return String.valueOf(value);
  • 0683 }
  • 0684
  • 0685 /**
  • 0686 * Returns a hash code for this <code>Integer</code>.
  • 0687 *
  • 0688 * @return a hash code value for this object, equal to the
  • 0689 * primitive <code>int</code> value represented by this
  • 0690 * <code>Integer</code> object.
  • 0691 */
  • 0692 public int hashCode() {
  • 0693 return value;
  • 0694 }
  • 0695
  • 0696 /**
  • 0697 * Compares this object to the specified object. The result is
  • 0698 * <code>true</code> if and only if the argument is not
  • 0699 * <code>null</code> and is an <code>Integer</code> object that
  • 0700 * contains the same <code>int</code> value as this object.
  • 0701 *
  • 0702 * @param obj the object to compare with.
  • 0703 * @return <code>true</code> if the objects are the same;
  • 0704 * <code>false</code> otherwise.
  • 0705 */
  • 0706 public boolean equals(Object obj) {
  • 0707 if (obj instanceof Integer) {
  • 0708 return value == ((Integer)obj).intValue();
  • 0709 }
  • 0710 return false;
  • 0711 }
  • 0712
  • 0713 /**
  • 0714 * Determines the integer value of the system property with the
  • 0715 * specified name.
  • 0716 * <p>
  • 0717 * The first argument is treated as the name of a system property.
  • 0718 * System properties are accessible through the
  • 0719 * {@link java.lang.System#getProperty(java.lang.String)} method. The
  • 0720 * string value of this property is then interpreted as an integer
  • 0721 * value and an <code>Integer</code> object representing this value is
  • 0722 * returned. Details of possible numeric formats can be found with
  • 0723 * the definition of <code>getProperty</code>.
  • 0724 * <p>
  • 0725 * If there is no property with the specified name, if the specified name
  • 0726 * is empty or <code>null</code>, or if the property does not have
  • 0727 * the correct numeric format, then <code>null</code> is returned.
  • 0728 * <p>
  • 0729 * In other words, this method returns an <code>Integer</code>
  • 0730 * object equal to the value of:
  • 0731 *
  • 0732 * <blockquote><code>
  • 0733 * getInteger(nm, null)
  • 0734 * </code></blockquote>
  • 0735 *
  • 0736 * @param nm property name.
  • 0737 * @return the <code>Integer</code> value of the property.
  • 0738 * @see java.lang.System#getProperty(java.lang.String)
  • 0739 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  • 0740 */
  • 0741 public static Integer getInteger(String nm) {
  • 0742 return getInteger(nm, null);
  • 0743 }
  • 0744
  • 0745 /**
  • 0746 * Determines the integer value of the system property with the
  • 0747 * specified name.
  • 0748 * <p>
  • 0749 * The first argument is treated as the name of a system property.
  • 0750 * System properties are accessible through the {@link
  • 0751 * java.lang.System#getProperty(java.lang.String)} method. The
  • 0752 * string value of this property is then interpreted as an integer
  • 0753 * value and an <code>Integer</code> object representing this value is
  • 0754 * returned. Details of possible numeric formats can be found with
  • 0755 * the definition of <code>getProperty</code>.
  • 0756 * <p>
  • 0757 * The second argument is the default value. An <code>Integer</code> object
  • 0758 * that represents the value of the second argument is returned if there
  • 0759 * is no property of the specified name, if the property does not have
  • 0760 * the correct numeric format, or if the specified name is empty or
  • 0761 * <code>null</code>.
  • 0762 * <p>
  • 0763 * In other words, this method returns an <code>Integer</code> object
  • 0764 * equal to the value of:
  • 0765 * <blockquote><code>
  • 0766 * getInteger(nm, new Integer(val))
  • 0767 * </code></blockquote>
  • 0768 * but in practice it may be implemented in a manner such as:
  • 0769 * <blockquote><pre>
  • 0770 * Integer result = getInteger(nm, null);
  • 0771 * return (result == null) ? new Integer(val) : result;
  • 0772 * </pre></blockquote>
  • 0773 * to avoid the unnecessary allocation of an <code>Integer</code>
  • 0774 * object when the default value is not needed.
  • 0775 *
  • 0776 * @param nm property name.
  • 0777 * @param val default value.
  • 0778 * @return the <code>Integer</code> value of the property.
  • 0779 * @see java.lang.System#getProperty(java.lang.String)
  • 0780 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  • 0781 */
  • 0782 public static Integer getInteger(String nm, int val) {
  • 0783 Integer result = getInteger(nm, null);
  • 0784 return (result == null) ? new Integer(val) : result;
  • 0785 }
  • 0786
  • 0787 /**
  • 0788 * Returns the integer value of the system property with the
  • 0789 * specified name. The first argument is treated as the name of a
  • 0790 * system property. System properties are accessible through the
  • 0791 * {@link java.lang.System#getProperty(java.lang.String)} method.
  • 0792 * The string value of this property is then interpreted as an
  • 0793 * integer value, as per the <code>Integer.decode</code> method,
  • 0794 * and an <code>Integer</code> object representing this value is
  • 0795 * returned.
  • 0796 * <p>
  • 0797 * <ul><li>If the property value begins with the two ASCII characters
  • 0798 * <code>0x</code> or the ASCII character <code>#</code>, not
  • 0799 * followed by a minus sign, then the rest of it is parsed as a
  • 0800 * hexadecimal integer exactly as by the method
  • 0801 * {@link #valueOf(java.lang.String, int)} with radix 16.
  • 0802 * <li>If the property value begins with the ASCII character
  • 0803 * <code>0</code> followed by another character, it is parsed as an
  • 0804 * octal integer exactly as by the method
  • 0805 * {@link #valueOf(java.lang.String, int)} with radix 8.
  • 0806 * <li>Otherwise, the property value is parsed as a decimal integer
  • 0807 * exactly as by the method {@link #valueOf(java.lang.String, int)}
  • 0808 * with radix 10.
  • 0809 * </ul><p>
  • 0810 * The second argument is the default value. The default value is
  • 0811 * returned if there is no property of the specified name, if the
  • 0812 * property does not have the correct numeric format, or if the
  • 0813 * specified name is empty or <code>null</code>.
  • 0814 *
  • 0815 * @param nm property name.
  • 0816 * @param val default value.
  • 0817 * @return the <code>Integer</code> value of the property.
  • 0818 * @see java.lang.System#getProperty(java.lang.String)
  • 0819 * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  • 0820 * @see java.lang.Integer#decode
  • 0821 */
  • 0822 public static Integer getInteger(String nm, Integer val) {
  • 0823 String v = null;
  • 0824 try {
  • 0825 v = System.getProperty(nm);
  • 0826 } catch (IllegalArgumentException e) {
  • 0827 } catch (NullPointerException e) {
  • 0828 }
  • 0829 if (v != null) {
  • 0830 try {
  • 0831 return Integer.decode(v);
  • 0832 } catch (NumberFormatException e) {
  • 0833 }
  • 0834 }
  • 0835 return val;
  • 0836 }
  • 0837
  • 0838 /**
  • 0839 * Decodes a <code>String</code> into an <code>Integer</code>.
  • 0840 * Accepts decimal, hexadecimal, and octal numbers given
  • 0841 * by the following grammar:
  • 0842 *
  • 0843 * <blockquote>
  • 0844 * <dl>
  • 0845 * <dt><i>DecodableString:</i>
  • 0846 * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
  • 0847 * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
  • 0848 * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
  • 0849 * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
  • 0850 * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
  • 0851 * <p>
  • 0852 * <dt><i>Sign:</i>
  • 0853 * <dd><code>-</code>
  • 0854 * </dl>
  • 0855 * </blockquote>
  • 0856 *
  • 0857 * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
  • 0858 * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
  • 0859 * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
  • 0860 * Language Specification</a>.
  • 0861 * <p>
  • 0862 * The sequence of characters following an (optional) negative
  • 0863 * sign and/or radix specifier ("<code>0x</code>",
  • 0864 * "<code>0X</code>", "<code>#</code>", or
  • 0865 * leading zero) is parsed as by the <code>Integer.parseInt</code>
  • 0866 * method with the indicated radix (10, 16, or 8). This sequence
  • 0867 * of characters must represent a positive value or a {@link
  • 0868 * NumberFormatException} will be thrown. The result is negated
  • 0869 * if first character of the specified <code>String</code> is the
  • 0870 * minus sign. No whitespace characters are permitted in the
  • 0871 * <code>String</code>.
  • 0872 *
  • 0873 * @param nm the <code>String</code> to decode.
  • 0874 * @return a <code>Integer</code> object holding the <code>int</code>
  • 0875 * value represented by <code>nm</code>
  • 0876 * @exception NumberFormatException if the <code>String</code> does not
  • 0877 * contain a parsable integer.
  • 0878 * @see java.lang.Integer#parseInt(java.lang.String, int)
  • 0879 */
  • 0880 public static Integer decode(String nm) throws NumberFormatException {
  • 0881 int radix = 10;
  • 0882 int index = 0;
  • 0883 boolean negative = false;
  • 0884 Integer result;
  • 0885
  • 0886 // Handle minus sign, if present
  • 0887 if (nm.startsWith("-")) {
  • 0888 negative = true;
  • 0889 index++;
  • 0890 }
  • 0891
  • 0892 // Handle radix specifier, if present
  • 0893 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  • 0894 index += 2;
  • 0895 radix = 16;
  • 0896 }
  • 0897 else if (nm.startsWith("#", index)) {
  • 0898 index ++;
  • 0899 radix = 16;
  • 0900 }
  • 0901 else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  • 0902 index ++;
  • 0903 radix = 8;
  • 0904 }
  • 0905
  • 0906 if (nm.startsWith("-", index))
  • 0907 throw new NumberFormatException("Negative sign in wrong position");
  • 0908
  • 0909 try {
  • 0910 result = Integer.valueOf(nm.substring(index), radix);
  • 0911 result = negative ? new Integer(-result.intValue()) : result;
  • 0912 } catch (NumberFormatException e) {
  • 0913 // If number is Integer.MIN_VALUE, we'll end up here. The next line
  • 0914 // handles this case, and causes any genuine format error to be
  • 0915 // rethrown.
  • 0916 String constant = negative ? new String("-" + nm.substring(index))
  • 0917 : nm.substring(index);
  • 0918 result = Integer.valueOf(constant, radix);
  • 0919 }
  • 0920 return result;
  • 0921 }
  • 0922
  • 0923 /**
  • 0924 * Compares two <code>Integer</code> objects numerically.
  • 0925 *
  • 0926 * @param anotherInteger the <code>Integer</code> to be compared.
  • 0927 * @return the value <code>0</code> if this <code>Integer</code> is
  • 0928 * equal to the argument <code>Integer</code>; a value less than
  • 0929 * <code>0</code> if this <code>Integer</code> is numerically less
  • 0930 * than the argument <code>Integer</code>; and a value greater
  • 0931 * than <code>0</code> if this <code>Integer</code> is numerically
  • 0932 * greater than the argument <code>Integer</code> (signed
  • 0933 * comparison).
  • 0934 * @since 1.2
  • 0935 */
  • 0936 public int compareTo(Integer anotherInteger) {
  • 0937 int thisVal = this.value;
  • 0938 int anotherVal = anotherInteger.value;
  • 0939 return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  • 0940 }
  • 0941
  • 0942
  • 0943 // Bit twiddling
  • 0944
  • 0945 /**
  • 0946 * The number of bits used to represent an <tt>int</tt> value in two's
  • 0947 * complement binary form.
  • 0948 *
  • 0949 * @since 1.5
  • 0950 */
  • 0951 public static final int SIZE = 32;
  • 0952
  • 0953 /**
  • 0954 * Returns an <tt>int</tt> value with at most a single one-bit, in the
  • 0955 * position of the highest-order ("leftmost") one-bit in the specified
  • 0956 * <tt>int</tt> value. Returns zero if the specified value has no
  • 0957 * one-bits in its two's complement binary representation, that is, if it
  • 0958 * is equal to zero.
  • 0959 *
  • 0960 * @return an <tt>int</tt> value with a single one-bit, in the position
  • 0961 * of the highest-order one-bit in the specified value, or zero if
  • 0962 * the specified value is itself equal to zero.
  • 0963 * @since 1.5
  • 0964 */
  • 0965 public static int highestOneBit(int i) {
  • 0966 // HD, Figure 3-1
  • 0967 i |= (i >> 1);
  • 0968 i |= (i >> 2);
  • 0969 i |= (i >> 4);
  • 0970 i |= (i >> 8);
  • 0971 i |= (i >> 16);
  • 0972 return i - (i >>> 1);
  • 0973 }
  • 0974
  • 0975 /**
  • 0976 * Returns an <tt>int</tt> value with at most a single one-bit, in the
  • 0977 * position of the lowest-order ("rightmost") one-bit in the specified
  • 0978 * <tt>int</tt> value. Returns zero if the specified value has no
  • 0979 * one-bits in its two's complement binary representation, that is, if it
  • 0980 * is equal to zero.
  • 0981 *
  • 0982 * @return an <tt>int</tt> value with a single one-bit, in the position
  • 0983 * of the lowest-order one-bit in the specified value, or zero if
  • 0984 * the specified value is itself equal to zero.
  • 0985 * @since 1.5
  • 0986 */
  • 0987 public static int lowestOneBit(int i) {
  • 0988 // HD, Section 2-1
  • 0989 return i & -i;
  • 0990 }
  • 0991
  • 0992 /**
  • 0993 * Returns the number of zero bits preceding the highest-order
  • 0994 * ("leftmost") one-bit in the two's complement binary representation
  • 0995 * of the specified <tt>int</tt> value. Returns 32 if the
  • 0996 * specified value has no one-bits in its two's complement representation,
  • 0997 * in other words if it is equal to zero.
  • 0998 *
  • 0999 * <p>Note that this method is closely related to the logarithm base 2.
  • 1000 * For all positive <tt>int</tt> values x:
  • 1001 * <ul>
  • 1002 * <li>floor(log<sub>2</sub>(x)) = <tt>31 - numberOfLeadingZeros(x)</tt>
  • 1003 * <li>ceil(log<sub>2</sub>(x)) = <tt>32 - numberOfLeadingZeros(x - 1)</tt>
  • 1004 * </ul>
  • 1005 *
  • 1006 * @return the number of zero bits preceding the highest-order
  • 1007 * ("leftmost") one-bit in the two's complement binary representation
  • 1008 * of the specified <tt>int</tt> value, or 32 if the value
  • 1009 * is equal to zero.
  • 1010 * @since 1.5
  • 1011 */
  • 1012 public static int numberOfLeadingZeros(int i) {
  • 1013 // HD, Figure 5-6
  • 1014 if (i == 0)
  • 1015 return 32;
  • 1016 int n = 1;
  • 1017 if (i >>> 16 == 0) { n += 16; i <<= 16; }
  • 1018 if (i >>> 24 == 0) { n += 8; i <<= 8; }
  • 1019 if (i >>> 28 == 0) { n += 4; i <<= 4; }
  • 1020 if (i >>> 30 == 0) { n += 2; i <<= 2; }
  • 1021 n -= i >>> 31;
  • 1022 return n;
  • 1023 }
  • 1024
  • 1025 /**
  • 1026 * Returns the number of zero bits following the lowest-order ("rightmost")
  • 1027 * one-bit in the two's complement binary representation of the specified
  • 1028 * <tt>int</tt> value. Returns 32 if the specified value has no
  • 1029 * one-bits in its two's complement representation, in other words if it is
  • 1030 * equal to zero.
  • 1031 *
  • 1032 * @return the number of zero bits following the lowest-order ("rightmost")
  • 1033 * one-bit in the two's complement binary representation of the
  • 1034 * specified <tt>int</tt> value, or 32 if the value is equal
  • 1035 * to zero.
  • 1036 * @since 1.5
  • 1037 */
  • 1038 public static int numberOfTrailingZeros(int i) {
  • 1039 // HD, Figure 5-14
  • 1040 int y;
  • 1041 if (i == 0) return 32;
  • 1042 int n = 31;
  • 1043 y = i <<16; if (y != 0) { n = n -16; i = y; }
  • 1044 y = i << 8; if (y != 0) { n = n - 8; i = y; }
  • 1045 y = i << 4; if (y != 0) { n = n - 4; i = y; }
  • 1046 y = i << 2; if (y != 0) { n = n - 2; i = y; }
  • 1047 return n - ((i << 1) >>> 31);
  • 1048 }
  • 1049
  • 1050 /**
  • 1051 * Returns the number of one-bits in the two's complement binary
  • 1052 * representation of the specified <tt>int</tt> value. This function is
  • 1053 * sometimes referred to as the <i>population count</i>.
  • 1054 *
  • 1055 * @return the number of one-bits in the two's complement binary
  • 1056 * representation of the specified <tt>int</tt> value.
  • 1057 * @since 1.5
  • 1058 */
  • 1059 public static int bitCount(int i) {
  • 1060 // HD, Figure 5-2
  • 1061 i = i - ((i >>> 1) & 0x55555555);
  • 1062 i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
  • 1063 i = (i + (i >>> 4)) & 0x0f0f0f0f;
  • 1064 i = i + (i >>> 8);
  • 1065 i = i + (i >>> 16);
  • 1066 return i & 0x3f;
  • 1067 }
  • 1068
  • 1069 /**
  • 1070 * Returns the value obtained by rotating the two's complement binary
  • 1071 * representation of the specified <tt>int</tt> value left by the
  • 1072 * specified number of bits. (Bits shifted out of the left hand, or
  • 1073 * high-order, side reenter on the right, or low-order.)
  • 1074 *
  • 1075 * <p>Note that left rotation with a negative distance is equivalent to
  • 1076 * right rotation: <tt>rotateLeft(val, -distance) == rotateRight(val,
  • 1077 * distance)</tt>. Note also that rotation by any multiple of 32 is a
  • 1078 * no-op, so all but the last five bits of the rotation distance can be
  • 1079 * ignored, even if the distance is negative: <tt>rotateLeft(val,
  • 1080 * distance) == rotateLeft(val, distance & 0x1F)</tt>.
  • 1081 *
  • 1082 * @return the value obtained by rotating the two's complement binary
  • 1083 * representation of the specified <tt>int</tt> value left by the
  • 1084 * specified number of bits.
  • 1085 * @since 1.5
  • 1086 */
  • 1087 public static int rotateLeft(int i, int distance) {
  • 1088 return (i << distance) | (i >>> -distance);
  • 1089 }
  • 1090
  • 1091 /**
  • 1092 * Returns the value obtained by rotating the two's complement binary
  • 1093 * representation of the specified <tt>int</tt> value right by the
  • 1094 * specified number of bits. (Bits shifted out of the right hand, or
  • 1095 * low-order, side reenter on the left, or high-order.)
  • 1096 *
  • 1097 * <p>Note that right rotation with a negative distance is equivalent to
  • 1098 * left rotation: <tt>rotateRight(val, -distance) == rotateLeft(val,
  • 1099 * distance)</tt>. Note also that rotation by any multiple of 32 is a
  • 1100 * no-op, so all but the last five bits of the rotation distance can be
  • 1101 * ignored, even if the distance is negative: <tt>rotateRight(val,
  • 1102 * distance) == rotateRight(val, distance & 0x1F)</tt>.
  • 1103 *
  • 1104 * @return the value obtained by rotating the two's complement binary
  • 1105 * representation of the specified <tt>int</tt> value right by the
  • 1106 * specified number of bits.
  • 1107 * @since 1.5
  • 1108 */
  • 1109 public static int rotateRight(int i, int distance) {
  • 1110 return (i >>> distance) | (i << -distance);
  • 1111 }
  • 1112
  • 1113 /**
  • 1114 * Returns the value obtained by reversing the order of the bits in the
  • 1115 * two's complement binary representation of the specified <tt>int</tt>
  • 1116 * value.
  • 1117 *
  • 1118 * @return the value obtained by reversing order of the bits in the
  • 1119 * specified <tt>int</tt> value.
  • 1120 * @since 1.5
  • 1121 */
  • 1122 public static int reverse(int i) {
  • 1123 // HD, Figure 7-1
  • 1124 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
  • 1125 i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
  • 1126 i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
  • 1127 i = (i << 24) | ((i & 0xff00) << 8) |
  • 1128 ((i >>> 8) & 0xff00) | (i >>> 24);
  • 1129 return i;
  • 1130 }
  • 1131
  • 1132 /**
  • 1133 * Returns the signum function of the specified <tt>int</tt> value. (The
  • 1134 * return value is -1 if the specified value is negative; 0 if the
  • 1135 * specified value is zero; and 1 if the specified value is positive.)
  • 1136 *
  • 1137 * @return the signum function of the specified <tt>int</tt> value.
  • 1138 * @since 1.5
  • 1139 */
  • 1140 public static int signum(int i) {
  • 1141 // HD, Section 2-7
  • 1142 return (i >> 31) | (-i >>> 31);
  • 1143 }
  • 1144
  • 1145 /**
  • 1146 * Returns the value obtained by reversing the order of the bytes in the
  • 1147 * two's complement representation of the specified <tt>int</tt> value.
  • 1148 *
  • 1149 * @return the value obtained by reversing the bytes in the specified
  • 1150 * <tt>int</tt> value.
  • 1151 * @since 1.5
  • 1152 */
  • 1153 public static int reverseBytes(int i) {
  • 1154 return ((i >>> 24) ) |
  • 1155 ((i >> 8) & 0xFF00) |
  • 1156 ((i << 8) & 0xFF0000) |
  • 1157 ((i << 24));
  • 1158 }
  • 1159
  • 1160 /** use serialVersionUID from JDK 1.0.2 for interoperability */
  • 1161 private static final long serialVersionUID = 1360826667806852920L;
  • 1162}

文件:Integer.java
包名:java.lang
类名:Integer
继承:Number
接口:[Comparable]