1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package java.lang; 19 20 /** 21 * The wrapper for the primitive type {@code int}. 22 * <p> 23 * As with the specification, this implementation relies on code laid out in <a 24 * href="http://www.hackersdelight.org/">Henry S. Warren, Jr.'s Hacker's 25 * Delight, (Addison Wesley, 2002)</a> as well as <a 26 * href="http://aggregate.org/MAGIC/">The Aggregate's Magic Algorithms</a>. 27 * 28 * @see java.lang.Number 29 * @since 1.1 30 */ 31 public final class Integer extends Number implements Comparable<Integer> { 32 33 private static final long serialVersionUID = 1360826667806852920L; 34 35 /** 36 * The value which the receiver represents. 37 */ 38 private final int value; 39 40 /** 41 * Constant for the maximum {@code int} value, 2<sup>31</sup>-1. 42 */ 43 public static final int MAX_VALUE = 0x7FFFFFFF; 44 45 /** 46 * Constant for the minimum {@code int} value, -2<sup>31</sup>. 47 */ 48 public static final int MIN_VALUE = 0x80000000; 49 50 /** 51 * Constant for the number of bits needed to represent an {@code int} in 52 * two's complement form. 53 * 54 * @since 1.5 55 */ 56 public static final int SIZE = 32; 57 58 /* 59 * Progressively smaller decimal order of magnitude that can be represented 60 * by an instance of Integer. Used to help compute the String 61 * representation. 62 */ 63 private static final int[] decimalScale = new int[] { 1000000000, 100000000, 64 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1 }; 65 66 /** 67 * The {@link Class} object that represents the primitive type {@code int}. 68 */ 69 @SuppressWarnings("unchecked") 70 public static final Class<Integer> TYPE = (Class<Integer>) new int[0] 71 .getClass().getComponentType(); 72 73 // Note: This can't be set to "int.class", since *that* is 74 // defined to be "java.lang.Integer.TYPE"; 75 76 /** 77 * Constructs a new {@code Integer} with the specified primitive integer 78 * value. 79 * 80 * @param value 81 * the primitive integer value to store in the new instance. 82 */ Integer(int value)83 public Integer(int value) { 84 this.value = value; 85 } 86 87 /** 88 * Constructs a new {@code Integer} from the specified string. 89 * 90 * @param string 91 * the string representation of an integer value. 92 * @throws NumberFormatException 93 * if {@code string} can not be decoded into an integer value. 94 * @see #parseInt(String) 95 */ Integer(String string)96 public Integer(String string) throws NumberFormatException { 97 this(parseInt(string)); 98 } 99 100 @Override byteValue()101 public byte byteValue() { 102 return (byte) value; 103 } 104 105 /** 106 * Compares this object to the specified integer object to determine their 107 * relative order. 108 * 109 * @param object 110 * the integer object to compare this object to. 111 * @return a negative value if the value of this integer is less than the 112 * value of {@code object}; 0 if the value of this integer and the 113 * value of {@code object} are equal; a positive value if the value 114 * of this integer is greater than the value of {@code object}. 115 * @see java.lang.Comparable 116 * @since 1.2 117 */ compareTo(Integer object)118 public int compareTo(Integer object) { 119 return value > object.value ? 1 : (value < object.value ? -1 : 0); 120 } 121 122 /** 123 * Parses the specified string and returns a {@code Integer} instance if the 124 * string can be decoded into an integer value. The string may be an 125 * optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), 126 * octal ("0..."), or decimal ("...") representation of an integer. 127 * 128 * @param string 129 * a string representation of an integer value. 130 * @return an {@code Integer} containing the value represented by 131 * {@code string}. 132 * @throws NumberFormatException 133 * if {@code string} can not be parsed as an integer value. 134 */ decode(String string)135 public static Integer decode(String string) throws NumberFormatException { 136 int length = string.length(), i = 0; 137 if (length == 0) { 138 // BEGIN android-changed 139 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 140 // END android-changed 141 } 142 char firstDigit = string.charAt(i); 143 boolean negative = firstDigit == '-'; 144 if (negative) { 145 if (length == 1) { 146 // BEGIN android-changed 147 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 148 // END android-changed 149 } 150 firstDigit = string.charAt(++i); 151 } 152 153 int base = 10; 154 if (firstDigit == '0') { 155 if (++i == length) { 156 return valueOf(0); 157 } 158 if ((firstDigit = string.charAt(i)) == 'x' || firstDigit == 'X') { 159 if (++i == length) { 160 // BEGIN android-changed 161 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 162 // END android-changed 163 } 164 base = 16; 165 } else { 166 base = 8; 167 } 168 } else if (firstDigit == '#') { 169 if (++i == length) { 170 // BEGIN android-changed 171 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 172 // END android-changed 173 } 174 base = 16; 175 } 176 177 int result = parse(string, i, base, negative); 178 return valueOf(result); 179 } 180 181 @Override doubleValue()182 public double doubleValue() { 183 return value; 184 } 185 186 /** 187 * Compares this instance with the specified object and indicates if they 188 * are equal. In order to be equal, {@code o} must be an instance of 189 * {@code Integer} and have the same integer value as this object. 190 * 191 * @param o 192 * the object to compare this integer with. 193 * @return {@code true} if the specified object is equal to this 194 * {@code Integer}; {@code false} otherwise. 195 */ 196 @Override equals(Object o)197 public boolean equals(Object o) { 198 return (o instanceof Integer) 199 && (value == ((Integer) o).value); 200 } 201 202 @Override floatValue()203 public float floatValue() { 204 return value; 205 } 206 207 /** 208 * Returns the {@code Integer} value of the system property identified by 209 * {@code string}. Returns {@code null} if {@code string} is {@code null} 210 * or empty, if the property can not be found or if its value can not be 211 * parsed as an integer. 212 * 213 * @param string 214 * the name of the requested system property. 215 * @return the requested property's value as an {@code Integer} or 216 * {@code null}. 217 */ getInteger(String string)218 public static Integer getInteger(String string) { 219 if (string == null || string.length() == 0) { 220 return null; 221 } 222 String prop = System.getProperty(string); 223 if (prop == null) { 224 return null; 225 } 226 try { 227 return decode(prop); 228 } catch (NumberFormatException ex) { 229 return null; 230 } 231 } 232 233 /** 234 * Returns the {@code Integer} value of the system property identified by 235 * {@code string}. Returns the specified default value if {@code string} is 236 * {@code null} or empty, if the property can not be found or if its value 237 * can not be parsed as an integer. 238 * 239 * @param string 240 * the name of the requested system property. 241 * @param defaultValue 242 * the default value that is returned if there is no integer 243 * system property with the requested name. 244 * @return the requested property's value as an {@code Integer} or the 245 * default value. 246 */ getInteger(String string, int defaultValue)247 public static Integer getInteger(String string, int defaultValue) { 248 if (string == null || string.length() == 0) { 249 return valueOf(defaultValue); 250 } 251 String prop = System.getProperty(string); 252 if (prop == null) { 253 return valueOf(defaultValue); 254 } 255 try { 256 return decode(prop); 257 } catch (NumberFormatException ex) { 258 return valueOf(defaultValue); 259 } 260 } 261 262 /** 263 * Returns the {@code Integer} value of the system property identified by 264 * {@code string}. Returns the specified default value if {@code string} is 265 * {@code null} or empty, if the property can not be found or if its value 266 * can not be parsed as an integer. 267 * 268 * @param string 269 * the name of the requested system property. 270 * @param defaultValue 271 * the default value that is returned if there is no integer 272 * system property with the requested name. 273 * @return the requested property's value as an {@code Integer} or the 274 * default value. 275 */ getInteger(String string, Integer defaultValue)276 public static Integer getInteger(String string, Integer defaultValue) { 277 if (string == null || string.length() == 0) { 278 return defaultValue; 279 } 280 String prop = System.getProperty(string); 281 if (prop == null) { 282 return defaultValue; 283 } 284 try { 285 return decode(prop); 286 } catch (NumberFormatException ex) { 287 return defaultValue; 288 } 289 } 290 291 @Override hashCode()292 public int hashCode() { 293 return value; 294 } 295 296 /** 297 * Gets the primitive value of this int. 298 * 299 * @return this object's primitive value. 300 */ 301 @Override intValue()302 public int intValue() { 303 return value; 304 } 305 306 @Override longValue()307 public long longValue() { 308 return value; 309 } 310 311 /** 312 * Parses the specified string as a signed decimal integer value. The ASCII 313 * character \u002d ('-') is recognized as the minus sign. 314 * 315 * @param string 316 * the string representation of an integer value. 317 * @return the primitive integer value represented by {@code string}. 318 * @throws NumberFormatException 319 * if {@code string} is {@code null}, has a length of zero or 320 * can not be parsed as an integer value. 321 */ parseInt(String string)322 public static int parseInt(String string) throws NumberFormatException { 323 return parseInt(string, 10); 324 } 325 326 /** 327 * Parses the specified string as a signed integer value using the specified 328 * radix. The ASCII character \u002d ('-') is recognized as the minus sign. 329 * 330 * @param string 331 * the string representation of an integer value. 332 * @param radix 333 * the radix to use when parsing. 334 * @return the primitive integer value represented by {@code string} using 335 * {@code radix}. 336 * @throws NumberFormatException 337 * if {@code string} is {@code null} or has a length of zero, 338 * {@code radix < Character.MIN_RADIX}, 339 * {@code radix > Character.MAX_RADIX}, or if {@code string} 340 * can not be parsed as an integer value. 341 */ parseInt(String string, int radix)342 public static int parseInt(String string, int radix) 343 throws NumberFormatException { 344 if (string == null || radix < Character.MIN_RADIX 345 || radix > Character.MAX_RADIX) { 346 // BEGIN android-changed 347 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 348 // END android-changed 349 } 350 int length = string.length(), i = 0; 351 if (length == 0) { 352 // BEGIN android-changed 353 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 354 // END android-changed 355 } 356 boolean negative = string.charAt(i) == '-'; 357 if (negative && ++i == length) { 358 // BEGIN android-changed 359 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 360 // END android-changed 361 } 362 363 return parse(string, i, radix, negative); 364 } 365 parse(String string, int offset, int radix, boolean negative)366 private static int parse(String string, int offset, int radix, 367 boolean negative) throws NumberFormatException { 368 int max = Integer.MIN_VALUE / radix; 369 int result = 0, length = string.length(); 370 while (offset < length) { 371 int digit = Character.digit(string.charAt(offset++), radix); 372 if (digit == -1) { 373 // BEGIN android-changed 374 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 375 // END android-changed 376 } 377 if (max > result) { 378 // BEGIN android-changed 379 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 380 // END android-changed 381 } 382 int next = result * radix - digit; 383 if (next > result) { 384 // BEGIN android-changed 385 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 386 // END android-changed 387 } 388 result = next; 389 } 390 if (!negative) { 391 result = -result; 392 if (result < 0) { 393 // BEGIN android-changed 394 throw new NumberFormatException("unable to parse '"+string+"' as integer"); 395 // END android-changed 396 } 397 } 398 return result; 399 } 400 401 @Override shortValue()402 public short shortValue() { 403 return (short) value; 404 } 405 406 /** 407 * Converts the specified integer into its binary string representation. The 408 * returned string is a concatenation of '0' and '1' characters. 409 * 410 * @param i 411 * the integer to convert. 412 * @return the binary string representation of {@code i}. 413 */ toBinaryString(int i)414 public static String toBinaryString(int i) { 415 int count = 1, j = i; 416 417 if (i < 0) { 418 count = 32; 419 } else { 420 while ((j >>>= 1) != 0) { 421 count++; 422 } 423 } 424 425 char[] buffer = new char[count]; 426 do { 427 buffer[--count] = (char) ((i & 1) + '0'); 428 i >>>= 1; 429 } while (count > 0); 430 return new String(0, buffer.length, buffer); 431 } 432 433 /** 434 * Converts the specified integer into its hexadecimal string 435 * representation. The returned string is a concatenation of characters from 436 * '0' to '9' and 'a' to 'f'. 437 * 438 * @param i 439 * the integer to convert. 440 * @return the hexadecimal string representation of {@code i}. 441 */ toHexString(int i)442 public static String toHexString(int i) { 443 int count = 1, j = i; 444 445 if (i < 0) { 446 count = 8; 447 } else { 448 while ((j >>>= 4) != 0) { 449 count++; 450 } 451 } 452 453 char[] buffer = new char[count]; 454 do { 455 int t = i & 15; 456 if (t > 9) { 457 t = t - 10 + 'a'; 458 } else { 459 t += '0'; 460 } 461 buffer[--count] = (char) t; 462 i >>>= 4; 463 } while (count > 0); 464 return new String(0, buffer.length, buffer); 465 } 466 467 /** 468 * Converts the specified integer into its octal string representation. The 469 * returned string is a concatenation of characters from '0' to '7'. 470 * 471 * @param i 472 * the integer to convert. 473 * @return the octal string representation of {@code i}. 474 */ toOctalString(int i)475 public static String toOctalString(int i) { 476 int count = 1, j = i; 477 478 if (i < 0) { 479 count = 11; 480 } else { 481 while ((j >>>= 3) != 0) { 482 count++; 483 } 484 } 485 486 char[] buffer = new char[count]; 487 do { 488 buffer[--count] = (char) ((i & 7) + '0'); 489 i >>>= 3; 490 } while (count > 0); 491 return new String(0, buffer.length, buffer); 492 } 493 494 @Override toString()495 public String toString() { 496 return Integer.toString(value); 497 } 498 499 /** 500 * Converts the specified integer into its decimal string representation. 501 * The returned string is a concatenation of a minus sign if the number is 502 * negative and characters from '0' to '9'. 503 * 504 * @param value 505 * the integer to convert. 506 * @return the decimal string representation of {@code value}. 507 */ toString(int value)508 public static String toString(int value) { 509 // BEGIN android-note 510 // cache the strings in the range 0..99 to save allocations? 511 // END android-note 512 if (value == 0) { 513 return "0"; //$NON-NLS-1$ 514 } 515 516 // Faster algorithm for smaller Integers 517 if (value < 1000 && value > -1000) { 518 char[] buffer = new char[4]; 519 int positive_value = value < 0 ? -value : value; 520 int first_digit = 0; 521 if (value < 0) { 522 buffer[0] = '-'; 523 first_digit++; 524 } 525 int last_digit = first_digit; 526 int quot = positive_value; 527 do { 528 int res = quot / 10; 529 int digit_value = quot - (res * 10); 530 digit_value += '0'; 531 buffer[last_digit++] = (char) digit_value; 532 quot = res; 533 } while (quot != 0); 534 535 int count = last_digit--; 536 do { 537 char tmp = buffer[last_digit]; 538 buffer[last_digit--] = buffer[first_digit]; 539 buffer[first_digit++] = tmp; 540 } while (first_digit < last_digit); 541 return new String(0, count, buffer); 542 } 543 if (value == MIN_VALUE) { 544 return "-2147483648";//$NON-NLS-1$ 545 } 546 547 char[] buffer = new char[11]; 548 int positive_value = value < 0 ? -value : value; 549 byte first_digit = 0; 550 if (value < 0) { 551 buffer[0] = '-'; 552 first_digit++; 553 } 554 byte last_digit = first_digit; 555 byte count; 556 int number; 557 boolean start = false; 558 for (int i = 0; i < 9; i++) { 559 count = 0; 560 if (positive_value < (number = decimalScale[i])) { 561 if (start) { 562 buffer[last_digit++] = '0'; 563 } 564 continue; 565 } 566 567 if (i > 0) { 568 number = (decimalScale[i] << 3); 569 if (positive_value >= number) { 570 positive_value -= number; 571 count += 8; 572 } 573 number = (decimalScale[i] << 2); 574 if (positive_value >= number) { 575 positive_value -= number; 576 count += 4; 577 } 578 } 579 number = (decimalScale[i] << 1); 580 if (positive_value >= number) { 581 positive_value -= number; 582 count += 2; 583 } 584 if (positive_value >= decimalScale[i]) { 585 positive_value -= decimalScale[i]; 586 count++; 587 } 588 if (count > 0 && !start) { 589 start = true; 590 } 591 if (start) { 592 buffer[last_digit++] = (char) (count + '0'); 593 } 594 } 595 596 buffer[last_digit++] = (char) (positive_value + '0'); 597 count = last_digit--; 598 return new String(0, count, buffer); 599 } 600 601 /** 602 * Converts the specified integer into a string representation based on the 603 * specified radix. The returned string is a concatenation of a minus sign 604 * if the number is negative and characters from '0' to '9' and 'a' to 'z', 605 * depending on the radix. If {@code radix} is not in the interval defined 606 * by {@code Character.MIN_RADIX} and {@code Character.MAX_RADIX} then 10 is 607 * used as the base for the conversion. 608 * 609 * @param i 610 * the integer to convert. 611 * @param radix 612 * the base to use for the conversion. 613 * @return the string representation of {@code i}. 614 */ 615 public static String toString(int i, int radix) { 616 // BEGIN android-note 617 // if radix==10, call thru to faster Integer.toString(int) ? 618 // only worthwhile if 10 is a popular parameter 619 // END android-note 620 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { 621 radix = 10; 622 } 623 if (i == 0) { 624 return "0"; //$NON-NLS-1$ 625 } 626 627 int count = 2, j = i; 628 boolean negative = i < 0; 629 if (!negative) { 630 count = 1; 631 j = -i; 632 } 633 while ((i /= radix) != 0) { 634 count++; 635 } 636 637 char[] buffer = new char[count]; 638 do { 639 int ch = 0 - (j % radix); 640 if (ch > 9) { 641 ch = ch - 10 + 'a'; 642 } else { 643 ch += '0'; 644 } 645 buffer[--count] = (char) ch; 646 } while ((j /= radix) != 0); 647 if (negative) { 648 buffer[0] = '-'; 649 } 650 return new String(0, buffer.length, buffer); 651 } 652 653 /** 654 * Parses the specified string as a signed decimal integer value. 655 * 656 * @param string 657 * the string representation of an integer value. 658 * @return an {@code Integer} instance containing the integer value 659 * represented by {@code string}. 660 * @throws NumberFormatException 661 * if {@code string} is {@code null}, has a length of zero or 662 * can not be parsed as an integer value. 663 * @see #parseInt(String) 664 */ 665 public static Integer valueOf(String string) throws NumberFormatException { 666 return valueOf(parseInt(string)); 667 } 668 669 /** 670 * Parses the specified string as a signed integer value using the specified 671 * radix. 672 * 673 * @param string 674 * the string representation of an integer value. 675 * @param radix 676 * the radix to use when parsing. 677 * @return an {@code Integer} instance containing the integer value 678 * represented by {@code string} using {@code radix}. 679 * @throws NumberFormatException 680 * if {@code string} is {@code null} or has a length of zero, 681 * {@code radix < Character.MIN_RADIX}, 682 * {@code radix > Character.MAX_RADIX}, or if {@code string} 683 * can not be parsed as an integer value. 684 * @see #parseInt(String, int) 685 */ 686 public static Integer valueOf(String string, int radix) 687 throws NumberFormatException { 688 return valueOf(parseInt(string, radix)); 689 } 690 691 /** 692 * Determines the highest (leftmost) bit of the specified integer that is 1 693 * and returns the bit mask value for that bit. This is also referred to as 694 * the Most Significant 1 Bit. Returns zero if the specified integer is 695 * zero. 696 * 697 * @param i 698 * the integer to examine. 699 * @return the bit mask indicating the highest 1 bit in {@code i}. 700 * @since 1.5 701 */ 702 public static int highestOneBit(int i) { 703 i |= (i >> 1); 704 i |= (i >> 2); 705 i |= (i >> 4); 706 i |= (i >> 8); 707 i |= (i >> 16); 708 return (i & ~(i >>> 1)); 709 } 710 711 /** 712 * Determines the lowest (rightmost) bit of the specified integer that is 1 713 * and returns the bit mask value for that bit. This is also referred 714 * to as the Least Significant 1 Bit. Returns zero if the specified integer 715 * is zero. 716 * 717 * @param i 718 * the integer to examine. 719 * @return the bit mask indicating the lowest 1 bit in {@code i}. 720 * @since 1.5 721 */ lowestOneBit(int i)722 public static int lowestOneBit(int i) { 723 return (i & (-i)); 724 } 725 726 /** 727 * Determines the number of leading zeros in the specified integer prior to 728 * the {@link #highestOneBit(int) highest one bit}. 729 * 730 * @param i 731 * the integer to examine. 732 * @return the number of leading zeros in {@code i}. 733 * @since 1.5 734 */ numberOfLeadingZeros(int i)735 public static int numberOfLeadingZeros(int i) { 736 i |= i >> 1; 737 i |= i >> 2; 738 i |= i >> 4; 739 i |= i >> 8; 740 i |= i >> 16; 741 return bitCount(~i); 742 } 743 744 /** 745 * Determines the number of trailing zeros in the specified integer after 746 * the {@link #lowestOneBit(int) lowest one bit}. 747 * 748 * @param i 749 * the integer to examine. 750 * @return the number of trailing zeros in {@code i}. 751 * @since 1.5 752 */ numberOfTrailingZeros(int i)753 public static int numberOfTrailingZeros(int i) { 754 return bitCount((i & -i) - 1); 755 } 756 757 /** 758 * Counts the number of 1 bits in the specified integer; this is also 759 * referred to as population count. 760 * 761 * @param i 762 * the integer to examine. 763 * @return the number of 1 bits in {@code i}. 764 * @since 1.5 765 */ bitCount(int i)766 public static int bitCount(int i) { 767 i -= ((i >> 1) & 0x55555555); 768 i = (i & 0x33333333) + ((i >> 2) & 0x33333333); 769 i = (((i >> 4) + i) & 0x0F0F0F0F); 770 i += (i >> 8); 771 i += (i >> 16); 772 return (i & 0x0000003F); 773 } 774 775 /** 776 * Rotates the bits of the specified integer to the left by the specified 777 * number of bits. 778 * 779 * @param i 780 * the integer value to rotate left. 781 * @param distance 782 * the number of bits to rotate. 783 * @return the rotated value. 784 * @since 1.5 785 */ rotateLeft(int i, int distance)786 public static int rotateLeft(int i, int distance) { 787 if (distance == 0) { 788 return i; 789 } 790 /* 791 * According to JLS3, 15.19, the right operand of a shift is always 792 * implicitly masked with 0x1F, which the negation of 'distance' is 793 * taking advantage of. 794 */ 795 return ((i << distance) | (i >>> (-distance))); 796 } 797 798 /** 799 * Rotates the bits of the specified integer to the right by the specified 800 * number of bits. 801 * 802 * @param i 803 * the integer value to rotate right. 804 * @param distance 805 * the number of bits to rotate. 806 * @return the rotated value. 807 * @since 1.5 808 */ rotateRight(int i, int distance)809 public static int rotateRight(int i, int distance) { 810 if (distance == 0) { 811 return i; 812 } 813 /* 814 * According to JLS3, 15.19, the right operand of a shift is always 815 * implicitly masked with 0x1F, which the negation of 'distance' is 816 * taking advantage of. 817 */ 818 return ((i >>> distance) | (i << (-distance))); 819 } 820 821 /** 822 * Reverses the order of the bytes of the specified integer. 823 * 824 * @param i 825 * the integer value for which to reverse the byte order. 826 * @return the reversed value. 827 * @since 1.5 828 */ reverseBytes(int i)829 public static int reverseBytes(int i) { 830 int b3 = i >>> 24; 831 int b2 = (i >>> 8) & 0xFF00; 832 int b1 = (i & 0xFF00) << 8; 833 int b0 = i << 24; 834 return (b0 | b1 | b2 | b3); 835 } 836 837 /** 838 * Reverses the order of the bits of the specified integer. 839 * 840 * @param i 841 * the integer value for which to reverse the bit order. 842 * @return the reversed value. 843 * @since 1.5 844 */ reverse(int i)845 public static int reverse(int i) { 846 // From Hacker's Delight, 7-1, Figure 7-1 847 i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555; 848 i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333; 849 i = (i & 0x0F0F0F0F) << 4 | (i >> 4) & 0x0F0F0F0F; 850 return reverseBytes(i); 851 } 852 853 /** 854 * Returns the value of the {@code signum} function for the specified 855 * integer. 856 * 857 * @param i 858 * the integer value to check. 859 * @return -1 if {@code i} is negative, 1 if {@code i} is positive, 0 if 860 * {@code i} is zero. 861 * @since 1.5 862 */ signum(int i)863 public static int signum(int i) { 864 return (i == 0 ? 0 : (i < 0 ? -1 : 1)); 865 } 866 867 /** 868 * Returns a {@code Integer} instance for the specified integer value. 869 * <p> 870 * If it is not necessary to get a new {@code Integer} instance, it is 871 * recommended to use this method instead of the constructor, since it 872 * maintains a cache of instances which may result in better performance. 873 * 874 * @param i 875 * the integer value to store in the instance. 876 * @return a {@code Integer} instance containing {@code i}. 877 * @since 1.5 878 */ valueOf(int i)879 public static Integer valueOf(int i) { 880 if (i < -128 || i > 127) { 881 return new Integer(i); 882 } 883 return valueOfCache.CACHE [i+128]; 884 885 } 886 887 static class valueOfCache { 888 /** 889 * <p> 890 * A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing. 891 */ 892 static final Integer[] CACHE = new Integer[256]; 893 894 static { 895 for(int i=-128; i<=127; i++) { 896 CACHE[i+128] = new Integer(i); 897 } 898 } 899 } 900 } 901