jaroslav@49: /* jaroslav@49: * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved. jaroslav@49: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@49: * jaroslav@49: * This code is free software; you can redistribute it and/or modify it jaroslav@49: * under the terms of the GNU General Public License version 2 only, as jaroslav@49: * published by the Free Software Foundation. Oracle designates this jaroslav@49: * particular file as subject to the "Classpath" exception as provided jaroslav@49: * by Oracle in the LICENSE file that accompanied this code. jaroslav@49: * jaroslav@49: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@49: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@49: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@49: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@49: * accompanied this code). jaroslav@49: * jaroslav@49: * You should have received a copy of the GNU General Public License version jaroslav@49: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@49: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@49: * jaroslav@49: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@49: * or visit www.oracle.com if you need additional information or have any jaroslav@49: * questions. jaroslav@49: */ jaroslav@49: jaroslav@49: package java.lang; jaroslav@49: jaroslav@49: import java.util.Properties; jaroslav@49: jaroslav@49: /** jaroslav@49: * The {@code Integer} class wraps a value of the primitive type jaroslav@49: * {@code int} in an object. An object of type {@code Integer} jaroslav@49: * contains a single field whose type is {@code int}. jaroslav@49: * jaroslav@49: *

In addition, this class provides several methods for converting jaroslav@49: * an {@code int} to a {@code String} and a {@code String} to an jaroslav@49: * {@code int}, as well as other constants and methods useful when jaroslav@49: * dealing with an {@code int}. jaroslav@49: * jaroslav@49: *

Implementation note: The implementations of the "bit twiddling" jaroslav@49: * methods (such as {@link #highestOneBit(int) highestOneBit} and jaroslav@49: * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are jaroslav@49: * based on material from Henry S. Warren, Jr.'s Hacker's jaroslav@49: * Delight, (Addison Wesley, 2002). jaroslav@49: * jaroslav@49: * @author Lee Boynton jaroslav@49: * @author Arthur van Hoff jaroslav@49: * @author Josh Bloch jaroslav@49: * @author Joseph D. Darcy jaroslav@49: * @since JDK1.0 jaroslav@49: */ jaroslav@49: public final class Integer extends Number implements Comparable { jaroslav@49: /** jaroslav@49: * A constant holding the minimum value an {@code int} can jaroslav@49: * have, -231. jaroslav@49: */ jaroslav@49: public static final int MIN_VALUE = 0x80000000; jaroslav@49: jaroslav@49: /** jaroslav@49: * A constant holding the maximum value an {@code int} can jaroslav@49: * have, 231-1. jaroslav@49: */ jaroslav@49: public static final int MAX_VALUE = 0x7fffffff; jaroslav@49: jaroslav@49: /** jaroslav@49: * The {@code Class} instance representing the primitive type jaroslav@49: * {@code int}. jaroslav@49: * jaroslav@49: * @since JDK1.1 jaroslav@49: */ jaroslav@49: public static final Class TYPE = (Class) Class.getPrimitiveClass("int"); jaroslav@49: jaroslav@49: /** jaroslav@49: * All possible chars for representing a number as a String jaroslav@49: */ jaroslav@49: final static char[] digits = { jaroslav@49: '0' , '1' , '2' , '3' , '4' , '5' , jaroslav@49: '6' , '7' , '8' , '9' , 'a' , 'b' , jaroslav@49: 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , jaroslav@49: 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , jaroslav@49: 'o' , 'p' , 'q' , 'r' , 's' , 't' , jaroslav@49: 'u' , 'v' , 'w' , 'x' , 'y' , 'z' jaroslav@49: }; jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a string representation of the first argument in the jaroslav@49: * radix specified by the second argument. jaroslav@49: * jaroslav@49: *

If the radix is smaller than {@code Character.MIN_RADIX} jaroslav@49: * or larger than {@code Character.MAX_RADIX}, then the radix jaroslav@49: * {@code 10} is used instead. jaroslav@49: * jaroslav@49: *

If the first argument is negative, the first element of the jaroslav@49: * result is the ASCII minus character {@code '-'} jaroslav@49: * ('\u002D'). If the first argument is not jaroslav@49: * negative, no sign character appears in the result. jaroslav@49: * jaroslav@49: *

The remaining characters of the result represent the magnitude jaroslav@49: * of the first argument. If the magnitude is zero, it is jaroslav@49: * represented by a single zero character {@code '0'} jaroslav@49: * ('\u0030'); otherwise, the first character of jaroslav@49: * the representation of the magnitude will not be the zero jaroslav@49: * character. The following ASCII characters are used as digits: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code 0123456789abcdefghijklmnopqrstuvwxyz} jaroslav@49: *
jaroslav@49: * jaroslav@49: * These are '\u0030' through jaroslav@49: * '\u0039' and '\u0061' through jaroslav@49: * '\u007A'. If {@code radix} is jaroslav@49: * N, then the first N of these characters jaroslav@49: * are used as radix-N digits in the order shown. Thus, jaroslav@49: * the digits for hexadecimal (radix 16) are jaroslav@49: * {@code 0123456789abcdef}. If uppercase letters are jaroslav@49: * desired, the {@link java.lang.String#toUpperCase()} method may jaroslav@49: * be called on the result: jaroslav@49: * jaroslav@49: *
jaroslav@49: * {@code Integer.toString(n, 16).toUpperCase()} jaroslav@49: *
jaroslav@49: * jaroslav@49: * @param i an integer to be converted to a string. jaroslav@49: * @param radix the radix to use in the string representation. jaroslav@49: * @return a string representation of the argument in the specified radix. jaroslav@49: * @see java.lang.Character#MAX_RADIX jaroslav@49: * @see java.lang.Character#MIN_RADIX jaroslav@49: */ jaroslav@49: public static String toString(int i, int radix) { jaroslav@49: jaroslav@49: if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) jaroslav@49: radix = 10; jaroslav@49: jaroslav@49: /* Use the faster version */ jaroslav@49: if (radix == 10) { jaroslav@49: return toString(i); jaroslav@49: } jaroslav@49: jaroslav@49: char buf[] = new char[33]; jaroslav@49: boolean negative = (i < 0); jaroslav@49: int charPos = 32; jaroslav@49: jaroslav@49: if (!negative) { jaroslav@49: i = -i; jaroslav@49: } jaroslav@49: jaroslav@49: while (i <= -radix) { jaroslav@49: buf[charPos--] = digits[-(i % radix)]; jaroslav@49: i = i / radix; jaroslav@49: } jaroslav@49: buf[charPos] = digits[-i]; jaroslav@49: jaroslav@49: if (negative) { jaroslav@49: buf[--charPos] = '-'; jaroslav@49: } jaroslav@49: jaroslav@49: return new String(buf, charPos, (33 - charPos)); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a string representation of the integer argument as an jaroslav@49: * unsigned integer in base 16. jaroslav@49: * jaroslav@49: *

The unsigned integer value is the argument plus 232 jaroslav@49: * if the argument is negative; otherwise, it is equal to the jaroslav@49: * argument. This value is converted to a string of ASCII digits jaroslav@49: * in hexadecimal (base 16) with no extra leading jaroslav@49: * {@code 0}s. If the unsigned magnitude is zero, it is jaroslav@49: * represented by a single zero character {@code '0'} jaroslav@49: * ('\u0030'); otherwise, the first character of jaroslav@49: * the representation of the unsigned magnitude will not be the jaroslav@49: * zero character. The following characters are used as jaroslav@49: * hexadecimal digits: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code 0123456789abcdef} jaroslav@49: *
jaroslav@49: * jaroslav@49: * These are the characters '\u0030' through jaroslav@49: * '\u0039' and '\u0061' through jaroslav@49: * '\u0066'. If uppercase letters are jaroslav@49: * desired, the {@link java.lang.String#toUpperCase()} method may jaroslav@49: * be called on the result: jaroslav@49: * jaroslav@49: *
jaroslav@49: * {@code Integer.toHexString(n).toUpperCase()} jaroslav@49: *
jaroslav@49: * jaroslav@49: * @param i an integer to be converted to a string. jaroslav@49: * @return the string representation of the unsigned integer value jaroslav@49: * represented by the argument in hexadecimal (base 16). jaroslav@49: * @since JDK1.0.2 jaroslav@49: */ jaroslav@49: public static String toHexString(int i) { jaroslav@49: return toUnsignedString(i, 4); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a string representation of the integer argument as an jaroslav@49: * unsigned integer in base 8. jaroslav@49: * jaroslav@49: *

The unsigned integer value is the argument plus 232 jaroslav@49: * if the argument is negative; otherwise, it is equal to the jaroslav@49: * argument. This value is converted to a string of ASCII digits jaroslav@49: * in octal (base 8) with no extra leading {@code 0}s. jaroslav@49: * jaroslav@49: *

If the unsigned magnitude is zero, it is represented by a jaroslav@49: * single zero character {@code '0'} jaroslav@49: * ('\u0030'); otherwise, the first character of jaroslav@49: * the representation of the unsigned magnitude will not be the jaroslav@49: * zero character. The following characters are used as octal jaroslav@49: * digits: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code 01234567} jaroslav@49: *
jaroslav@49: * jaroslav@49: * These are the characters '\u0030' through jaroslav@49: * '\u0037'. jaroslav@49: * jaroslav@49: * @param i an integer to be converted to a string. jaroslav@49: * @return the string representation of the unsigned integer value jaroslav@49: * represented by the argument in octal (base 8). jaroslav@49: * @since JDK1.0.2 jaroslav@49: */ jaroslav@49: public static String toOctalString(int i) { jaroslav@49: return toUnsignedString(i, 3); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a string representation of the integer argument as an jaroslav@49: * unsigned integer in base 2. jaroslav@49: * jaroslav@49: *

The unsigned integer value is the argument plus 232 jaroslav@49: * if the argument is negative; otherwise it is equal to the jaroslav@49: * argument. This value is converted to a string of ASCII digits jaroslav@49: * in binary (base 2) with no extra leading {@code 0}s. jaroslav@49: * If the unsigned magnitude is zero, it is represented by a jaroslav@49: * single zero character {@code '0'} jaroslav@49: * ('\u0030'); otherwise, the first character of jaroslav@49: * the representation of the unsigned magnitude will not be the jaroslav@49: * zero character. The characters {@code '0'} jaroslav@49: * ('\u0030') and {@code '1'} jaroslav@49: * ('\u0031') are used as binary digits. jaroslav@49: * jaroslav@49: * @param i an integer to be converted to a string. jaroslav@49: * @return the string representation of the unsigned integer value jaroslav@49: * represented by the argument in binary (base 2). jaroslav@49: * @since JDK1.0.2 jaroslav@49: */ jaroslav@49: public static String toBinaryString(int i) { jaroslav@49: return toUnsignedString(i, 1); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Convert the integer to an unsigned number. jaroslav@49: */ jaroslav@49: private static String toUnsignedString(int i, int shift) { jaroslav@49: char[] buf = new char[32]; jaroslav@49: int charPos = 32; jaroslav@49: int radix = 1 << shift; jaroslav@49: int mask = radix - 1; jaroslav@49: do { jaroslav@49: buf[--charPos] = digits[i & mask]; jaroslav@49: i >>>= shift; jaroslav@49: } while (i != 0); jaroslav@49: jaroslav@49: return new String(buf, charPos, (32 - charPos)); jaroslav@49: } jaroslav@49: jaroslav@49: jaroslav@49: final static char [] DigitTens = { jaroslav@49: '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', jaroslav@49: '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', jaroslav@49: '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', jaroslav@49: '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', jaroslav@49: '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', jaroslav@49: '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', jaroslav@49: '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', jaroslav@49: '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', jaroslav@49: '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', jaroslav@49: '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', jaroslav@49: } ; jaroslav@49: jaroslav@49: final static char [] DigitOnes = { jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', jaroslav@49: } ; jaroslav@49: jaroslav@49: // I use the "invariant division by multiplication" trick to jaroslav@49: // accelerate Integer.toString. In particular we want to jaroslav@49: // avoid division by 10. jaroslav@49: // jaroslav@49: // The "trick" has roughly the same performance characteristics jaroslav@49: // as the "classic" Integer.toString code on a non-JIT VM. jaroslav@49: // The trick avoids .rem and .div calls but has a longer code jaroslav@49: // path and is thus dominated by dispatch overhead. In the jaroslav@49: // JIT case the dispatch overhead doesn't exist and the jaroslav@49: // "trick" is considerably faster than the classic code. jaroslav@49: // jaroslav@49: // TODO-FIXME: convert (x * 52429) into the equiv shift-add jaroslav@49: // sequence. jaroslav@49: // jaroslav@49: // RE: Division by Invariant Integers using Multiplication jaroslav@49: // T Gralund, P Montgomery jaroslav@49: // ACM PLDI 1994 jaroslav@49: // jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a {@code String} object representing the jaroslav@49: * specified integer. The argument is converted to signed decimal jaroslav@49: * representation and returned as a string, exactly as if the jaroslav@49: * argument and radix 10 were given as arguments to the {@link jaroslav@49: * #toString(int, int)} method. jaroslav@49: * jaroslav@49: * @param i an integer to be converted. jaroslav@49: * @return a string representation of the argument in base 10. jaroslav@49: */ jaroslav@49: public static String toString(int i) { jaroslav@49: if (i == Integer.MIN_VALUE) jaroslav@49: return "-2147483648"; jaroslav@49: int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); jaroslav@49: char[] buf = new char[size]; jaroslav@49: getChars(i, size, buf); jaroslav@49: return new String(0, size, buf); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Places characters representing the integer i into the jaroslav@49: * character array buf. The characters are placed into jaroslav@49: * the buffer backwards starting with the least significant jaroslav@49: * digit at the specified index (exclusive), and working jaroslav@49: * backwards from there. jaroslav@49: * jaroslav@49: * Will fail if i == Integer.MIN_VALUE jaroslav@49: */ jaroslav@49: static void getChars(int i, int index, char[] buf) { jaroslav@49: int q, r; jaroslav@49: int charPos = index; jaroslav@49: char sign = 0; jaroslav@49: jaroslav@49: if (i < 0) { jaroslav@49: sign = '-'; jaroslav@49: i = -i; jaroslav@49: } jaroslav@49: jaroslav@49: // Generate two digits per iteration jaroslav@49: while (i >= 65536) { jaroslav@49: q = i / 100; jaroslav@49: // really: r = i - (q * 100); jaroslav@49: r = i - ((q << 6) + (q << 5) + (q << 2)); jaroslav@49: i = q; jaroslav@49: buf [--charPos] = DigitOnes[r]; jaroslav@49: buf [--charPos] = DigitTens[r]; jaroslav@49: } jaroslav@49: jaroslav@49: // Fall thru to fast mode for smaller numbers jaroslav@49: // assert(i <= 65536, i); jaroslav@49: for (;;) { jaroslav@49: q = (i * 52429) >>> (16+3); jaroslav@49: r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... jaroslav@49: buf [--charPos] = digits [r]; jaroslav@49: i = q; jaroslav@49: if (i == 0) break; jaroslav@49: } jaroslav@49: if (sign != 0) { jaroslav@49: buf [--charPos] = sign; jaroslav@49: } jaroslav@49: } jaroslav@49: jaroslav@49: final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, jaroslav@49: 99999999, 999999999, Integer.MAX_VALUE }; jaroslav@49: jaroslav@49: // Requires positive x jaroslav@49: static int stringSize(int x) { jaroslav@49: for (int i=0; ; i++) jaroslav@49: if (x <= sizeTable[i]) jaroslav@49: return i+1; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Parses the string argument as a signed integer in the radix jaroslav@49: * specified by the second argument. The characters in the string jaroslav@49: * must all be digits of the specified radix (as determined by jaroslav@49: * whether {@link java.lang.Character#digit(char, int)} returns a jaroslav@49: * nonnegative value), except that the first character may be an jaroslav@49: * ASCII minus sign {@code '-'} ('\u002D') to jaroslav@49: * indicate a negative value or an ASCII plus sign {@code '+'} jaroslav@49: * ('\u002B') to indicate a positive value. The jaroslav@49: * resulting integer value is returned. jaroslav@49: * jaroslav@49: *

An exception of type {@code NumberFormatException} is jaroslav@49: * thrown if any of the following situations occurs: jaroslav@49: *

jaroslav@49: * jaroslav@49: *

Examples: jaroslav@49: *

jaroslav@49:      * parseInt("0", 10) returns 0
jaroslav@49:      * parseInt("473", 10) returns 473
jaroslav@49:      * parseInt("+42", 10) returns 42
jaroslav@49:      * parseInt("-0", 10) returns 0
jaroslav@49:      * parseInt("-FF", 16) returns -255
jaroslav@49:      * parseInt("1100110", 2) returns 102
jaroslav@49:      * parseInt("2147483647", 10) returns 2147483647
jaroslav@49:      * parseInt("-2147483648", 10) returns -2147483648
jaroslav@49:      * parseInt("2147483648", 10) throws a NumberFormatException
jaroslav@49:      * parseInt("99", 8) throws a NumberFormatException
jaroslav@49:      * parseInt("Kona", 10) throws a NumberFormatException
jaroslav@49:      * parseInt("Kona", 27) returns 411787
jaroslav@49:      * 
jaroslav@49: * jaroslav@49: * @param s the {@code String} containing the integer jaroslav@49: * representation to be parsed jaroslav@49: * @param radix the radix to be used while parsing {@code s}. jaroslav@49: * @return the integer represented by the string argument in the jaroslav@49: * specified radix. jaroslav@49: * @exception NumberFormatException if the {@code String} jaroslav@49: * does not contain a parsable {@code int}. jaroslav@49: */ jaroslav@49: public static int parseInt(String s, int radix) jaroslav@49: throws NumberFormatException jaroslav@49: { jaroslav@49: /* jaroslav@49: * WARNING: This method may be invoked early during VM initialization jaroslav@49: * before IntegerCache is initialized. Care must be taken to not use jaroslav@49: * the valueOf method. jaroslav@49: */ jaroslav@49: jaroslav@49: if (s == null) { jaroslav@49: throw new NumberFormatException("null"); jaroslav@49: } jaroslav@49: jaroslav@49: if (radix < Character.MIN_RADIX) { jaroslav@49: throw new NumberFormatException("radix " + radix + jaroslav@49: " less than Character.MIN_RADIX"); jaroslav@49: } jaroslav@49: jaroslav@49: if (radix > Character.MAX_RADIX) { jaroslav@49: throw new NumberFormatException("radix " + radix + jaroslav@49: " greater than Character.MAX_RADIX"); jaroslav@49: } jaroslav@49: jaroslav@49: int result = 0; jaroslav@49: boolean negative = false; jaroslav@49: int i = 0, len = s.length(); jaroslav@49: int limit = -Integer.MAX_VALUE; jaroslav@49: int multmin; jaroslav@49: int digit; jaroslav@49: jaroslav@49: if (len > 0) { jaroslav@49: char firstChar = s.charAt(0); jaroslav@49: if (firstChar < '0') { // Possible leading "+" or "-" jaroslav@49: if (firstChar == '-') { jaroslav@49: negative = true; jaroslav@49: limit = Integer.MIN_VALUE; jaroslav@49: } else if (firstChar != '+') jaroslav@49: throw NumberFormatException.forInputString(s); jaroslav@49: jaroslav@49: if (len == 1) // Cannot have lone "+" or "-" jaroslav@49: throw NumberFormatException.forInputString(s); jaroslav@49: i++; jaroslav@49: } jaroslav@49: multmin = limit / radix; jaroslav@49: while (i < len) { jaroslav@49: // Accumulating negatively avoids surprises near MAX_VALUE jaroslav@49: digit = Character.digit(s.charAt(i++),radix); jaroslav@49: if (digit < 0) { jaroslav@49: throw NumberFormatException.forInputString(s); jaroslav@49: } jaroslav@49: if (result < multmin) { jaroslav@49: throw NumberFormatException.forInputString(s); jaroslav@49: } jaroslav@49: result *= radix; jaroslav@49: if (result < limit + digit) { jaroslav@49: throw NumberFormatException.forInputString(s); jaroslav@49: } jaroslav@49: result -= digit; jaroslav@49: } jaroslav@49: } else { jaroslav@49: throw NumberFormatException.forInputString(s); jaroslav@49: } jaroslav@49: return negative ? result : -result; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Parses the string argument as a signed decimal integer. The jaroslav@49: * characters in the string must all be decimal digits, except jaroslav@49: * that the first character may be an ASCII minus sign {@code '-'} jaroslav@49: * ('\u002D') to indicate a negative value or an jaroslav@49: * ASCII plus sign {@code '+'} ('\u002B') to jaroslav@49: * indicate a positive value. The resulting integer value is jaroslav@49: * returned, exactly as if the argument and the radix 10 were jaroslav@49: * given as arguments to the {@link #parseInt(java.lang.String, jaroslav@49: * int)} method. jaroslav@49: * jaroslav@49: * @param s a {@code String} containing the {@code int} jaroslav@49: * representation to be parsed jaroslav@49: * @return the integer value represented by the argument in decimal. jaroslav@49: * @exception NumberFormatException if the string does not contain a jaroslav@49: * parsable integer. jaroslav@49: */ jaroslav@49: public static int parseInt(String s) throws NumberFormatException { jaroslav@49: return parseInt(s,10); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns an {@code Integer} object holding the value jaroslav@49: * extracted from the specified {@code String} when parsed jaroslav@49: * with the radix given by the second argument. The first argument jaroslav@49: * is interpreted as representing a signed integer in the radix jaroslav@49: * specified by the second argument, exactly as if the arguments jaroslav@49: * were given to the {@link #parseInt(java.lang.String, int)} jaroslav@49: * method. The result is an {@code Integer} object that jaroslav@49: * represents the integer value specified by the string. jaroslav@49: * jaroslav@49: *

In other words, this method returns an {@code Integer} jaroslav@49: * object equal to the value of: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code new Integer(Integer.parseInt(s, radix))} jaroslav@49: *
jaroslav@49: * jaroslav@49: * @param s the string to be parsed. jaroslav@49: * @param radix the radix to be used in interpreting {@code s} jaroslav@49: * @return an {@code Integer} object holding the value jaroslav@49: * represented by the string argument in the specified jaroslav@49: * radix. jaroslav@49: * @exception NumberFormatException if the {@code String} jaroslav@49: * does not contain a parsable {@code int}. jaroslav@49: */ jaroslav@49: public static Integer valueOf(String s, int radix) throws NumberFormatException { jaroslav@49: return Integer.valueOf(parseInt(s,radix)); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns an {@code Integer} object holding the jaroslav@49: * value of the specified {@code String}. The argument is jaroslav@49: * interpreted as representing a signed decimal integer, exactly jaroslav@49: * as if the argument were given to the {@link jaroslav@49: * #parseInt(java.lang.String)} method. The result is an jaroslav@49: * {@code Integer} object that represents the integer value jaroslav@49: * specified by the string. jaroslav@49: * jaroslav@49: *

In other words, this method returns an {@code Integer} jaroslav@49: * object equal to the value of: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code new Integer(Integer.parseInt(s))} jaroslav@49: *
jaroslav@49: * jaroslav@49: * @param s the string to be parsed. jaroslav@49: * @return an {@code Integer} object holding the value jaroslav@49: * represented by the string argument. jaroslav@49: * @exception NumberFormatException if the string cannot be parsed jaroslav@49: * as an integer. jaroslav@49: */ jaroslav@49: public static Integer valueOf(String s) throws NumberFormatException { jaroslav@49: return Integer.valueOf(parseInt(s, 10)); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Cache to support the object identity semantics of autoboxing for values between jaroslav@49: * -128 and 127 (inclusive) as required by JLS. jaroslav@49: * jaroslav@49: * The cache is initialized on first usage. The size of the cache jaroslav@49: * may be controlled by the -XX:AutoBoxCacheMax= option. jaroslav@49: * During VM initialization, java.lang.Integer.IntegerCache.high property jaroslav@49: * may be set and saved in the private system properties in the jaroslav@49: * sun.misc.VM class. jaroslav@49: */ jaroslav@49: jaroslav@49: private static class IntegerCache { jaroslav@49: static final int low = -128; jaroslav@49: static final int high; jaroslav@49: static final Integer cache[]; jaroslav@49: jaroslav@49: static { jaroslav@49: // high value may be configured by property jaroslav@49: int h = 127; jaroslav@49: String integerCacheHighPropValue = jaroslav@49: sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); jaroslav@49: if (integerCacheHighPropValue != null) { jaroslav@49: int i = parseInt(integerCacheHighPropValue); jaroslav@49: i = Math.max(i, 127); jaroslav@49: // Maximum array size is Integer.MAX_VALUE jaroslav@49: h = Math.min(i, Integer.MAX_VALUE - (-low)); jaroslav@49: } jaroslav@49: high = h; jaroslav@49: jaroslav@49: cache = new Integer[(high - low) + 1]; jaroslav@49: int j = low; jaroslav@49: for(int k = 0; k < cache.length; k++) jaroslav@49: cache[k] = new Integer(j++); jaroslav@49: } jaroslav@49: jaroslav@49: private IntegerCache() {} jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns an {@code Integer} instance representing the specified jaroslav@49: * {@code int} value. If a new {@code Integer} instance is not jaroslav@49: * required, this method should generally be used in preference to jaroslav@49: * the constructor {@link #Integer(int)}, as this method is likely jaroslav@49: * to yield significantly better space and time performance by jaroslav@49: * caching frequently requested values. jaroslav@49: * jaroslav@49: * This method will always cache values in the range -128 to 127, jaroslav@49: * inclusive, and may cache other values outside of this range. jaroslav@49: * jaroslav@49: * @param i an {@code int} value. jaroslav@49: * @return an {@code Integer} instance representing {@code i}. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static Integer valueOf(int i) { jaroslav@49: assert IntegerCache.high >= 127; jaroslav@49: if (i >= IntegerCache.low && i <= IntegerCache.high) jaroslav@49: return IntegerCache.cache[i + (-IntegerCache.low)]; jaroslav@49: return new Integer(i); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * The value of the {@code Integer}. jaroslav@49: * jaroslav@49: * @serial jaroslav@49: */ jaroslav@49: private final int value; jaroslav@49: jaroslav@49: /** jaroslav@49: * Constructs a newly allocated {@code Integer} object that jaroslav@49: * represents the specified {@code int} value. jaroslav@49: * jaroslav@49: * @param value the value to be represented by the jaroslav@49: * {@code Integer} object. jaroslav@49: */ jaroslav@49: public Integer(int value) { jaroslav@49: this.value = value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Constructs a newly allocated {@code Integer} object that jaroslav@49: * represents the {@code int} value indicated by the jaroslav@49: * {@code String} parameter. The string is converted to an jaroslav@49: * {@code int} value in exactly the manner used by the jaroslav@49: * {@code parseInt} method for radix 10. jaroslav@49: * jaroslav@49: * @param s the {@code String} to be converted to an jaroslav@49: * {@code Integer}. jaroslav@49: * @exception NumberFormatException if the {@code String} does not jaroslav@49: * contain a parsable integer. jaroslav@49: * @see java.lang.Integer#parseInt(java.lang.String, int) jaroslav@49: */ jaroslav@49: public Integer(String s) throws NumberFormatException { jaroslav@49: this.value = parseInt(s, 10); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value of this {@code Integer} as a jaroslav@49: * {@code byte}. jaroslav@49: */ jaroslav@49: public byte byteValue() { jaroslav@49: return (byte)value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value of this {@code Integer} as a jaroslav@49: * {@code short}. jaroslav@49: */ jaroslav@49: public short shortValue() { jaroslav@49: return (short)value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value of this {@code Integer} as an jaroslav@49: * {@code int}. jaroslav@49: */ jaroslav@49: public int intValue() { jaroslav@49: return value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value of this {@code Integer} as a jaroslav@49: * {@code long}. jaroslav@49: */ jaroslav@49: public long longValue() { jaroslav@49: return (long)value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value of this {@code Integer} as a jaroslav@49: * {@code float}. jaroslav@49: */ jaroslav@49: public float floatValue() { jaroslav@49: return (float)value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value of this {@code Integer} as a jaroslav@49: * {@code double}. jaroslav@49: */ jaroslav@49: public double doubleValue() { jaroslav@49: return (double)value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a {@code String} object representing this jaroslav@49: * {@code Integer}'s value. The value is converted to signed jaroslav@49: * decimal representation and returned as a string, exactly as if jaroslav@49: * the integer value were given as an argument to the {@link jaroslav@49: * java.lang.Integer#toString(int)} method. jaroslav@49: * jaroslav@49: * @return a string representation of the value of this object in jaroslav@49: * base 10. jaroslav@49: */ jaroslav@49: public String toString() { jaroslav@49: return toString(value); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns a hash code for this {@code Integer}. jaroslav@49: * jaroslav@49: * @return a hash code value for this object, equal to the jaroslav@49: * primitive {@code int} value represented by this jaroslav@49: * {@code Integer} object. jaroslav@49: */ jaroslav@49: public int hashCode() { jaroslav@49: return value; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Compares this object to the specified object. The result is jaroslav@49: * {@code true} if and only if the argument is not jaroslav@49: * {@code null} and is an {@code Integer} object that jaroslav@49: * contains the same {@code int} value as this object. jaroslav@49: * jaroslav@49: * @param obj the object to compare with. jaroslav@49: * @return {@code true} if the objects are the same; jaroslav@49: * {@code false} otherwise. jaroslav@49: */ jaroslav@49: public boolean equals(Object obj) { jaroslav@49: if (obj instanceof Integer) { jaroslav@49: return value == ((Integer)obj).intValue(); jaroslav@49: } jaroslav@49: return false; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Determines the integer value of the system property with the jaroslav@49: * specified name. jaroslav@49: * jaroslav@49: *

The first argument is treated as the name of a system property. jaroslav@49: * System properties are accessible through the jaroslav@49: * {@link java.lang.System#getProperty(java.lang.String)} method. The jaroslav@49: * string value of this property is then interpreted as an integer jaroslav@49: * value and an {@code Integer} object representing this value is jaroslav@49: * returned. Details of possible numeric formats can be found with jaroslav@49: * the definition of {@code getProperty}. jaroslav@49: * jaroslav@49: *

If there is no property with the specified name, if the specified name jaroslav@49: * is empty or {@code null}, or if the property does not have jaroslav@49: * the correct numeric format, then {@code null} is returned. jaroslav@49: * jaroslav@49: *

In other words, this method returns an {@code Integer} jaroslav@49: * object equal to the value of: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code getInteger(nm, null)} jaroslav@49: *
jaroslav@49: * jaroslav@49: * @param nm property name. jaroslav@49: * @return the {@code Integer} value of the property. jaroslav@49: * @see java.lang.System#getProperty(java.lang.String) jaroslav@49: * @see java.lang.System#getProperty(java.lang.String, java.lang.String) jaroslav@49: */ jaroslav@49: public static Integer getInteger(String nm) { jaroslav@49: return getInteger(nm, null); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Determines the integer value of the system property with the jaroslav@49: * specified name. jaroslav@49: * jaroslav@49: *

The first argument is treated as the name of a system property. jaroslav@49: * System properties are accessible through the {@link jaroslav@49: * java.lang.System#getProperty(java.lang.String)} method. The jaroslav@49: * string value of this property is then interpreted as an integer jaroslav@49: * value and an {@code Integer} object representing this value is jaroslav@49: * returned. Details of possible numeric formats can be found with jaroslav@49: * the definition of {@code getProperty}. jaroslav@49: * jaroslav@49: *

The second argument is the default value. An {@code Integer} object jaroslav@49: * that represents the value of the second argument is returned if there jaroslav@49: * is no property of the specified name, if the property does not have jaroslav@49: * the correct numeric format, or if the specified name is empty or jaroslav@49: * {@code null}. jaroslav@49: * jaroslav@49: *

In other words, this method returns an {@code Integer} object jaroslav@49: * equal to the value of: jaroslav@49: * jaroslav@49: *

jaroslav@49: * {@code getInteger(nm, new Integer(val))} jaroslav@49: *
jaroslav@49: * jaroslav@49: * but in practice it may be implemented in a manner such as: jaroslav@49: * jaroslav@49: *
jaroslav@49:      * Integer result = getInteger(nm, null);
jaroslav@49:      * return (result == null) ? new Integer(val) : result;
jaroslav@49:      * 
jaroslav@49: * jaroslav@49: * to avoid the unnecessary allocation of an {@code Integer} jaroslav@49: * object when the default value is not needed. jaroslav@49: * jaroslav@49: * @param nm property name. jaroslav@49: * @param val default value. jaroslav@49: * @return the {@code Integer} value of the property. jaroslav@49: * @see java.lang.System#getProperty(java.lang.String) jaroslav@49: * @see java.lang.System#getProperty(java.lang.String, java.lang.String) jaroslav@49: */ jaroslav@49: public static Integer getInteger(String nm, int val) { jaroslav@49: Integer result = getInteger(nm, null); jaroslav@49: return (result == null) ? Integer.valueOf(val) : result; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the integer value of the system property with the jaroslav@49: * specified name. The first argument is treated as the name of a jaroslav@49: * system property. System properties are accessible through the jaroslav@49: * {@link java.lang.System#getProperty(java.lang.String)} method. jaroslav@49: * The string value of this property is then interpreted as an jaroslav@49: * integer value, as per the {@code Integer.decode} method, jaroslav@49: * and an {@code Integer} object representing this value is jaroslav@49: * returned. jaroslav@49: * jaroslav@49: * jaroslav@49: * jaroslav@49: *

The second argument is the default value. The default value is jaroslav@49: * returned if there is no property of the specified name, if the jaroslav@49: * property does not have the correct numeric format, or if the jaroslav@49: * specified name is empty or {@code null}. jaroslav@49: * jaroslav@49: * @param nm property name. jaroslav@49: * @param val default value. jaroslav@49: * @return the {@code Integer} value of the property. jaroslav@49: * @see java.lang.System#getProperty(java.lang.String) jaroslav@49: * @see java.lang.System#getProperty(java.lang.String, java.lang.String) jaroslav@49: * @see java.lang.Integer#decode jaroslav@49: */ jaroslav@49: public static Integer getInteger(String nm, Integer val) { jaroslav@49: String v = null; jaroslav@49: try { jaroslav@49: v = System.getProperty(nm); jaroslav@49: } catch (IllegalArgumentException e) { jaroslav@49: } catch (NullPointerException e) { jaroslav@49: } jaroslav@49: if (v != null) { jaroslav@49: try { jaroslav@49: return Integer.decode(v); jaroslav@49: } catch (NumberFormatException e) { jaroslav@49: } jaroslav@49: } jaroslav@49: return val; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Decodes a {@code String} into an {@code Integer}. jaroslav@49: * Accepts decimal, hexadecimal, and octal numbers given jaroslav@49: * by the following grammar: jaroslav@49: * jaroslav@49: *

jaroslav@49: *
jaroslav@49: *
DecodableString: jaroslav@49: *
Signopt DecimalNumeral jaroslav@49: *
Signopt {@code 0x} HexDigits jaroslav@49: *
Signopt {@code 0X} HexDigits jaroslav@49: *
Signopt {@code #} HexDigits jaroslav@49: *
Signopt {@code 0} OctalDigits jaroslav@49: *

jaroslav@49: *

Sign: jaroslav@49: *
{@code -} jaroslav@49: *
{@code +} jaroslav@49: *
jaroslav@49: *
jaroslav@49: * jaroslav@49: * DecimalNumeral, HexDigits, and OctalDigits jaroslav@49: * are as defined in section 3.10.1 of jaroslav@49: * The Java™ Language Specification, jaroslav@49: * except that underscores are not accepted between digits. jaroslav@49: * jaroslav@49: *

The sequence of characters following an optional jaroslav@49: * sign and/or radix specifier ("{@code 0x}", "{@code 0X}", jaroslav@49: * "{@code #}", or leading zero) is parsed as by the {@code jaroslav@49: * Integer.parseInt} method with the indicated radix (10, 16, or jaroslav@49: * 8). This sequence of characters must represent a positive jaroslav@49: * value or a {@link NumberFormatException} will be thrown. The jaroslav@49: * result is negated if first character of the specified {@code jaroslav@49: * String} is the minus sign. No whitespace characters are jaroslav@49: * permitted in the {@code String}. jaroslav@49: * jaroslav@49: * @param nm the {@code String} to decode. jaroslav@49: * @return an {@code Integer} object holding the {@code int} jaroslav@49: * value represented by {@code nm} jaroslav@49: * @exception NumberFormatException if the {@code String} does not jaroslav@49: * contain a parsable integer. jaroslav@49: * @see java.lang.Integer#parseInt(java.lang.String, int) jaroslav@49: */ jaroslav@49: public static Integer decode(String nm) throws NumberFormatException { jaroslav@49: int radix = 10; jaroslav@49: int index = 0; jaroslav@49: boolean negative = false; jaroslav@49: Integer result; jaroslav@49: jaroslav@49: if (nm.length() == 0) jaroslav@49: throw new NumberFormatException("Zero length string"); jaroslav@49: char firstChar = nm.charAt(0); jaroslav@49: // Handle sign, if present jaroslav@49: if (firstChar == '-') { jaroslav@49: negative = true; jaroslav@49: index++; jaroslav@49: } else if (firstChar == '+') jaroslav@49: index++; jaroslav@49: jaroslav@49: // Handle radix specifier, if present jaroslav@49: if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { jaroslav@49: index += 2; jaroslav@49: radix = 16; jaroslav@49: } jaroslav@49: else if (nm.startsWith("#", index)) { jaroslav@49: index ++; jaroslav@49: radix = 16; jaroslav@49: } jaroslav@49: else if (nm.startsWith("0", index) && nm.length() > 1 + index) { jaroslav@49: index ++; jaroslav@49: radix = 8; jaroslav@49: } jaroslav@49: jaroslav@49: if (nm.startsWith("-", index) || nm.startsWith("+", index)) jaroslav@49: throw new NumberFormatException("Sign character in wrong position"); jaroslav@49: jaroslav@49: try { jaroslav@49: result = Integer.valueOf(nm.substring(index), radix); jaroslav@49: result = negative ? Integer.valueOf(-result.intValue()) : result; jaroslav@49: } catch (NumberFormatException e) { jaroslav@49: // If number is Integer.MIN_VALUE, we'll end up here. The next line jaroslav@49: // handles this case, and causes any genuine format error to be jaroslav@49: // rethrown. jaroslav@49: String constant = negative ? ("-" + nm.substring(index)) jaroslav@49: : nm.substring(index); jaroslav@49: result = Integer.valueOf(constant, radix); jaroslav@49: } jaroslav@49: return result; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Compares two {@code Integer} objects numerically. jaroslav@49: * jaroslav@49: * @param anotherInteger the {@code Integer} to be compared. jaroslav@49: * @return the value {@code 0} if this {@code Integer} is jaroslav@49: * equal to the argument {@code Integer}; a value less than jaroslav@49: * {@code 0} if this {@code Integer} is numerically less jaroslav@49: * than the argument {@code Integer}; and a value greater jaroslav@49: * than {@code 0} if this {@code Integer} is numerically jaroslav@49: * greater than the argument {@code Integer} (signed jaroslav@49: * comparison). jaroslav@49: * @since 1.2 jaroslav@49: */ jaroslav@49: public int compareTo(Integer anotherInteger) { jaroslav@49: return compare(this.value, anotherInteger.value); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Compares two {@code int} values numerically. jaroslav@49: * The value returned is identical to what would be returned by: jaroslav@49: *

jaroslav@49:      *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
jaroslav@49:      * 
jaroslav@49: * jaroslav@49: * @param x the first {@code int} to compare jaroslav@49: * @param y the second {@code int} to compare jaroslav@49: * @return the value {@code 0} if {@code x == y}; jaroslav@49: * a value less than {@code 0} if {@code x < y}; and jaroslav@49: * a value greater than {@code 0} if {@code x > y} jaroslav@49: * @since 1.7 jaroslav@49: */ jaroslav@49: public static int compare(int x, int y) { jaroslav@49: return (x < y) ? -1 : ((x == y) ? 0 : 1); jaroslav@49: } jaroslav@49: jaroslav@49: jaroslav@49: // Bit twiddling jaroslav@49: jaroslav@49: /** jaroslav@49: * The number of bits used to represent an {@code int} value in two's jaroslav@49: * complement binary form. jaroslav@49: * jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static final int SIZE = 32; jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns an {@code int} value with at most a single one-bit, in the jaroslav@49: * position of the highest-order ("leftmost") one-bit in the specified jaroslav@49: * {@code int} value. Returns zero if the specified value has no jaroslav@49: * one-bits in its two's complement binary representation, that is, if it jaroslav@49: * is equal to zero. jaroslav@49: * jaroslav@49: * @return an {@code int} value with a single one-bit, in the position jaroslav@49: * of the highest-order one-bit in the specified value, or zero if jaroslav@49: * the specified value is itself equal to zero. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int highestOneBit(int i) { jaroslav@49: // HD, Figure 3-1 jaroslav@49: i |= (i >> 1); jaroslav@49: i |= (i >> 2); jaroslav@49: i |= (i >> 4); jaroslav@49: i |= (i >> 8); jaroslav@49: i |= (i >> 16); jaroslav@49: return i - (i >>> 1); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns an {@code int} value with at most a single one-bit, in the jaroslav@49: * position of the lowest-order ("rightmost") one-bit in the specified jaroslav@49: * {@code int} value. Returns zero if the specified value has no jaroslav@49: * one-bits in its two's complement binary representation, that is, if it jaroslav@49: * is equal to zero. jaroslav@49: * jaroslav@49: * @return an {@code int} value with a single one-bit, in the position jaroslav@49: * of the lowest-order one-bit in the specified value, or zero if jaroslav@49: * the specified value is itself equal to zero. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int lowestOneBit(int i) { jaroslav@49: // HD, Section 2-1 jaroslav@49: return i & -i; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the number of zero bits preceding the highest-order jaroslav@49: * ("leftmost") one-bit in the two's complement binary representation jaroslav@49: * of the specified {@code int} value. Returns 32 if the jaroslav@49: * specified value has no one-bits in its two's complement representation, jaroslav@49: * in other words if it is equal to zero. jaroslav@49: * jaroslav@49: *

Note that this method is closely related to the logarithm base 2. jaroslav@49: * For all positive {@code int} values x: jaroslav@49: *

jaroslav@49: * jaroslav@49: * @return the number of zero bits preceding the highest-order jaroslav@49: * ("leftmost") one-bit in the two's complement binary representation jaroslav@49: * of the specified {@code int} value, or 32 if the value jaroslav@49: * is equal to zero. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int numberOfLeadingZeros(int i) { jaroslav@49: // HD, Figure 5-6 jaroslav@49: if (i == 0) jaroslav@49: return 32; jaroslav@49: int n = 1; jaroslav@49: if (i >>> 16 == 0) { n += 16; i <<= 16; } jaroslav@49: if (i >>> 24 == 0) { n += 8; i <<= 8; } jaroslav@49: if (i >>> 28 == 0) { n += 4; i <<= 4; } jaroslav@49: if (i >>> 30 == 0) { n += 2; i <<= 2; } jaroslav@49: n -= i >>> 31; jaroslav@49: return n; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the number of zero bits following the lowest-order ("rightmost") jaroslav@49: * one-bit in the two's complement binary representation of the specified jaroslav@49: * {@code int} value. Returns 32 if the specified value has no jaroslav@49: * one-bits in its two's complement representation, in other words if it is jaroslav@49: * equal to zero. jaroslav@49: * jaroslav@49: * @return the number of zero bits following the lowest-order ("rightmost") jaroslav@49: * one-bit in the two's complement binary representation of the jaroslav@49: * specified {@code int} value, or 32 if the value is equal jaroslav@49: * to zero. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int numberOfTrailingZeros(int i) { jaroslav@49: // HD, Figure 5-14 jaroslav@49: int y; jaroslav@49: if (i == 0) return 32; jaroslav@49: int n = 31; jaroslav@49: y = i <<16; if (y != 0) { n = n -16; i = y; } jaroslav@49: y = i << 8; if (y != 0) { n = n - 8; i = y; } jaroslav@49: y = i << 4; if (y != 0) { n = n - 4; i = y; } jaroslav@49: y = i << 2; if (y != 0) { n = n - 2; i = y; } jaroslav@49: return n - ((i << 1) >>> 31); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the number of one-bits in the two's complement binary jaroslav@49: * representation of the specified {@code int} value. This function is jaroslav@49: * sometimes referred to as the population count. jaroslav@49: * jaroslav@49: * @return the number of one-bits in the two's complement binary jaroslav@49: * representation of the specified {@code int} value. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int bitCount(int i) { jaroslav@49: // HD, Figure 5-2 jaroslav@49: i = i - ((i >>> 1) & 0x55555555); jaroslav@49: i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); jaroslav@49: i = (i + (i >>> 4)) & 0x0f0f0f0f; jaroslav@49: i = i + (i >>> 8); jaroslav@49: i = i + (i >>> 16); jaroslav@49: return i & 0x3f; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value obtained by rotating the two's complement binary jaroslav@49: * representation of the specified {@code int} value left by the jaroslav@49: * specified number of bits. (Bits shifted out of the left hand, or jaroslav@49: * high-order, side reenter on the right, or low-order.) jaroslav@49: * jaroslav@49: *

Note that left rotation with a negative distance is equivalent to jaroslav@49: * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, jaroslav@49: * distance)}. Note also that rotation by any multiple of 32 is a jaroslav@49: * no-op, so all but the last five bits of the rotation distance can be jaroslav@49: * ignored, even if the distance is negative: {@code rotateLeft(val, jaroslav@49: * distance) == rotateLeft(val, distance & 0x1F)}. jaroslav@49: * jaroslav@49: * @return the value obtained by rotating the two's complement binary jaroslav@49: * representation of the specified {@code int} value left by the jaroslav@49: * specified number of bits. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int rotateLeft(int i, int distance) { jaroslav@49: return (i << distance) | (i >>> -distance); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value obtained by rotating the two's complement binary jaroslav@49: * representation of the specified {@code int} value right by the jaroslav@49: * specified number of bits. (Bits shifted out of the right hand, or jaroslav@49: * low-order, side reenter on the left, or high-order.) jaroslav@49: * jaroslav@49: *

Note that right rotation with a negative distance is equivalent to jaroslav@49: * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, jaroslav@49: * distance)}. Note also that rotation by any multiple of 32 is a jaroslav@49: * no-op, so all but the last five bits of the rotation distance can be jaroslav@49: * ignored, even if the distance is negative: {@code rotateRight(val, jaroslav@49: * distance) == rotateRight(val, distance & 0x1F)}. jaroslav@49: * jaroslav@49: * @return the value obtained by rotating the two's complement binary jaroslav@49: * representation of the specified {@code int} value right by the jaroslav@49: * specified number of bits. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int rotateRight(int i, int distance) { jaroslav@49: return (i >>> distance) | (i << -distance); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value obtained by reversing the order of the bits in the jaroslav@49: * two's complement binary representation of the specified {@code int} jaroslav@49: * value. jaroslav@49: * jaroslav@49: * @return the value obtained by reversing order of the bits in the jaroslav@49: * specified {@code int} value. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int reverse(int i) { jaroslav@49: // HD, Figure 7-1 jaroslav@49: i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; jaroslav@49: i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; jaroslav@49: i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; jaroslav@49: i = (i << 24) | ((i & 0xff00) << 8) | jaroslav@49: ((i >>> 8) & 0xff00) | (i >>> 24); jaroslav@49: return i; jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the signum function of the specified {@code int} value. (The jaroslav@49: * return value is -1 if the specified value is negative; 0 if the jaroslav@49: * specified value is zero; and 1 if the specified value is positive.) jaroslav@49: * jaroslav@49: * @return the signum function of the specified {@code int} value. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int signum(int i) { jaroslav@49: // HD, Section 2-7 jaroslav@49: return (i >> 31) | (-i >>> 31); jaroslav@49: } jaroslav@49: jaroslav@49: /** jaroslav@49: * Returns the value obtained by reversing the order of the bytes in the jaroslav@49: * two's complement representation of the specified {@code int} value. jaroslav@49: * jaroslav@49: * @return the value obtained by reversing the bytes in the specified jaroslav@49: * {@code int} value. jaroslav@49: * @since 1.5 jaroslav@49: */ jaroslav@49: public static int reverseBytes(int i) { jaroslav@49: return ((i >>> 24) ) | jaroslav@49: ((i >> 8) & 0xFF00) | jaroslav@49: ((i << 8) & 0xFF0000) | jaroslav@49: ((i << 24)); jaroslav@49: } jaroslav@49: jaroslav@49: /** use serialVersionUID from JDK 1.0.2 for interoperability */ jaroslav@49: private static final long serialVersionUID = 1360826667806852920L; jaroslav@49: }