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