0N/A/*
3261N/A * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
1019N/Aimport java.util.Properties;
1019N/A
0N/A/**
0N/A * The {@code Integer} class wraps a value of the primitive type
0N/A * {@code int} in an object. An object of type {@code Integer}
0N/A * contains a single field whose type is {@code int}.
0N/A *
0N/A * <p>In addition, this class provides several methods for converting
0N/A * an {@code int} to a {@code String} and a {@code String} to an
0N/A * {@code int}, as well as other constants and methods useful when
0N/A * dealing with an {@code int}.
0N/A *
0N/A * <p>Implementation note: The implementations of the "bit twiddling"
0N/A * methods (such as {@link #highestOneBit(int) highestOneBit} and
0N/A * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
0N/A * based on material from Henry S. Warren, Jr.'s <i>Hacker's
0N/A * Delight</i>, (Addison Wesley, 2002).
0N/A *
0N/A * @author Lee Boynton
0N/A * @author Arthur van Hoff
0N/A * @author Josh Bloch
0N/A * @author Joseph D. Darcy
0N/A * @since JDK1.0
0N/A */
0N/Apublic final class Integer extends Number implements Comparable<Integer> {
0N/A /**
0N/A * A constant holding the minimum value an {@code int} can
0N/A * have, -2<sup>31</sup>.
0N/A */
0N/A public static final int MIN_VALUE = 0x80000000;
0N/A
0N/A /**
0N/A * A constant holding the maximum value an {@code int} can
0N/A * have, 2<sup>31</sup>-1.
0N/A */
0N/A public static final int MAX_VALUE = 0x7fffffff;
0N/A
0N/A /**
0N/A * The {@code Class} instance representing the primitive type
0N/A * {@code int}.
0N/A *
0N/A * @since JDK1.1
0N/A */
0N/A public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
0N/A
0N/A /**
0N/A * All possible chars for representing a number as a String
0N/A */
0N/A final static char[] digits = {
0N/A '0' , '1' , '2' , '3' , '4' , '5' ,
0N/A '6' , '7' , '8' , '9' , 'a' , 'b' ,
0N/A 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
0N/A 'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
0N/A 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
0N/A 'u' , 'v' , 'w' , 'x' , 'y' , 'z'
0N/A };
0N/A
0N/A /**
0N/A * Returns a string representation of the first argument in the
0N/A * radix specified by the second argument.
0N/A *
0N/A * <p>If the radix is smaller than {@code Character.MIN_RADIX}
0N/A * or larger than {@code Character.MAX_RADIX}, then the radix
0N/A * {@code 10} is used instead.
0N/A *
0N/A * <p>If the first argument is negative, the first element of the
0N/A * result is the ASCII minus character {@code '-'}
0N/A * (<code>'&#92;u002D'</code>). If the first argument is not
0N/A * negative, no sign character appears in the result.
0N/A *
0N/A * <p>The remaining characters of the result represent the magnitude
0N/A * of the first argument. If the magnitude is zero, it is
0N/A * represented by a single zero character {@code '0'}
0N/A * (<code>'&#92;u0030'</code>); otherwise, the first character of
0N/A * the representation of the magnitude will not be the zero
0N/A * character. The following ASCII characters are used as digits:
0N/A *
0N/A * <blockquote>
0N/A * {@code 0123456789abcdefghijklmnopqrstuvwxyz}
0N/A * </blockquote>
0N/A *
0N/A * These are <code>'&#92;u0030'</code> through
0N/A * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
0N/A * <code>'&#92;u007A'</code>. If {@code radix} is
0N/A * <var>N</var>, then the first <var>N</var> of these characters
0N/A * are used as radix-<var>N</var> digits in the order shown. Thus,
0N/A * the digits for hexadecimal (radix 16) are
0N/A * {@code 0123456789abcdef}. If uppercase letters are
0N/A * desired, the {@link java.lang.String#toUpperCase()} method may
0N/A * be called on the result:
0N/A *
0N/A * <blockquote>
0N/A * {@code Integer.toString(n, 16).toUpperCase()}
0N/A * </blockquote>
0N/A *
0N/A * @param i an integer to be converted to a string.
0N/A * @param radix the radix to use in the string representation.
0N/A * @return a string representation of the argument in the specified radix.
0N/A * @see java.lang.Character#MAX_RADIX
0N/A * @see java.lang.Character#MIN_RADIX
0N/A */
0N/A public static String toString(int i, int radix) {
0N/A
0N/A if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
0N/A radix = 10;
0N/A
0N/A /* Use the faster version */
0N/A if (radix == 10) {
0N/A return toString(i);
0N/A }
0N/A
0N/A char buf[] = new char[33];
0N/A boolean negative = (i < 0);
0N/A int charPos = 32;
0N/A
0N/A if (!negative) {
0N/A i = -i;
0N/A }
0N/A
0N/A while (i <= -radix) {
0N/A buf[charPos--] = digits[-(i % radix)];
0N/A i = i / radix;
0N/A }
0N/A buf[charPos] = digits[-i];
0N/A
0N/A if (negative) {
0N/A buf[--charPos] = '-';
0N/A }
0N/A
0N/A return new String(buf, charPos, (33 - charPos));
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of the integer argument as an
0N/A * unsigned integer in base&nbsp;16.
0N/A *
0N/A * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
0N/A * if the argument is negative; otherwise, it is equal to the
0N/A * argument. This value is converted to a string of ASCII digits
0N/A * in hexadecimal (base&nbsp;16) with no extra leading
0N/A * {@code 0}s. If the unsigned magnitude is zero, it is
0N/A * represented by a single zero character {@code '0'}
0N/A * (<code>'&#92;u0030'</code>); otherwise, the first character of
0N/A * the representation of the unsigned magnitude will not be the
0N/A * zero character. The following characters are used as
0N/A * hexadecimal digits:
0N/A *
0N/A * <blockquote>
0N/A * {@code 0123456789abcdef}
0N/A * </blockquote>
0N/A *
0N/A * These are the characters <code>'&#92;u0030'</code> through
0N/A * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
0N/A * <code>'&#92;u0066'</code>. If uppercase letters are
0N/A * desired, the {@link java.lang.String#toUpperCase()} method may
0N/A * be called on the result:
0N/A *
0N/A * <blockquote>
0N/A * {@code Integer.toHexString(n).toUpperCase()}
0N/A * </blockquote>
0N/A *
0N/A * @param i an integer to be converted to a string.
0N/A * @return the string representation of the unsigned integer value
0N/A * represented by the argument in hexadecimal (base&nbsp;16).
0N/A * @since JDK1.0.2
0N/A */
0N/A public static String toHexString(int i) {
0N/A return toUnsignedString(i, 4);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of the integer argument as an
0N/A * unsigned integer in base&nbsp;8.
0N/A *
0N/A * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
0N/A * if the argument is negative; otherwise, it is equal to the
0N/A * argument. This value is converted to a string of ASCII digits
0N/A * in octal (base&nbsp;8) with no extra leading {@code 0}s.
0N/A *
0N/A * <p>If the unsigned magnitude is zero, it is represented by a
0N/A * single zero character {@code '0'}
0N/A * (<code>'&#92;u0030'</code>); otherwise, the first character of
0N/A * the representation of the unsigned magnitude will not be the
0N/A * zero character. The following characters are used as octal
0N/A * digits:
0N/A *
0N/A * <blockquote>
0N/A * {@code 01234567}
0N/A * </blockquote>
0N/A *
0N/A * These are the characters <code>'&#92;u0030'</code> through
0N/A * <code>'&#92;u0037'</code>.
0N/A *
0N/A * @param i an integer to be converted to a string.
0N/A * @return the string representation of the unsigned integer value
0N/A * represented by the argument in octal (base&nbsp;8).
0N/A * @since JDK1.0.2
0N/A */
0N/A public static String toOctalString(int i) {
0N/A return toUnsignedString(i, 3);
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of the integer argument as an
0N/A * unsigned integer in base&nbsp;2.
0N/A *
0N/A * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
0N/A * if the argument is negative; otherwise it is equal to the
0N/A * argument. This value is converted to a string of ASCII digits
0N/A * in binary (base&nbsp;2) with no extra leading {@code 0}s.
0N/A * If the unsigned magnitude is zero, it is represented by a
0N/A * single zero character {@code '0'}
0N/A * (<code>'&#92;u0030'</code>); otherwise, the first character of
0N/A * the representation of the unsigned magnitude will not be the
0N/A * zero character. The characters {@code '0'}
0N/A * (<code>'&#92;u0030'</code>) and {@code '1'}
0N/A * (<code>'&#92;u0031'</code>) are used as binary digits.
0N/A *
0N/A * @param i an integer to be converted to a string.
0N/A * @return the string representation of the unsigned integer value
0N/A * represented by the argument in binary (base&nbsp;2).
0N/A * @since JDK1.0.2
0N/A */
0N/A public static String toBinaryString(int i) {
0N/A return toUnsignedString(i, 1);
0N/A }
0N/A
0N/A /**
0N/A * Convert the integer to an unsigned number.
0N/A */
0N/A private static String toUnsignedString(int i, int shift) {
0N/A char[] buf = new char[32];
0N/A int charPos = 32;
0N/A int radix = 1 << shift;
0N/A int mask = radix - 1;
0N/A do {
0N/A buf[--charPos] = digits[i & mask];
0N/A i >>>= shift;
0N/A } while (i != 0);
0N/A
0N/A return new String(buf, charPos, (32 - charPos));
0N/A }
0N/A
0N/A
0N/A final static char [] DigitTens = {
0N/A '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
0N/A '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
0N/A '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
0N/A '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
0N/A '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
0N/A '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
0N/A '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
0N/A '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
0N/A '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
0N/A '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
0N/A } ;
0N/A
0N/A final static char [] DigitOnes = {
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A } ;
0N/A
0N/A // I use the "invariant division by multiplication" trick to
0N/A // accelerate Integer.toString. In particular we want to
0N/A // avoid division by 10.
0N/A //
0N/A // The "trick" has roughly the same performance characteristics
0N/A // as the "classic" Integer.toString code on a non-JIT VM.
0N/A // The trick avoids .rem and .div calls but has a longer code
0N/A // path and is thus dominated by dispatch overhead. In the
0N/A // JIT case the dispatch overhead doesn't exist and the
0N/A // "trick" is considerably faster than the classic code.
0N/A //
0N/A // TODO-FIXME: convert (x * 52429) into the equiv shift-add
0N/A // sequence.
0N/A //
0N/A // RE: Division by Invariant Integers using Multiplication
0N/A // T Gralund, P Montgomery
0N/A // ACM PLDI 1994
0N/A //
0N/A
0N/A /**
0N/A * Returns a {@code String} object representing the
0N/A * specified integer. The argument is converted to signed decimal
0N/A * representation and returned as a string, exactly as if the
0N/A * argument and radix 10 were given as arguments to the {@link
0N/A * #toString(int, int)} method.
0N/A *
0N/A * @param i an integer to be converted.
0N/A * @return a string representation of the argument in base&nbsp;10.
0N/A */
0N/A public static String toString(int i) {
0N/A if (i == Integer.MIN_VALUE)
0N/A return "-2147483648";
0N/A int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
0N/A char[] buf = new char[size];
0N/A getChars(i, size, buf);
5049N/A return new String(buf, true);
0N/A }
0N/A
0N/A /**
0N/A * Places characters representing the integer i into the
0N/A * character array buf. The characters are placed into
0N/A * the buffer backwards starting with the least significant
0N/A * digit at the specified index (exclusive), and working
0N/A * backwards from there.
0N/A *
0N/A * Will fail if i == Integer.MIN_VALUE
0N/A */
0N/A static void getChars(int i, int index, char[] buf) {
0N/A int q, r;
0N/A int charPos = index;
0N/A char sign = 0;
0N/A
0N/A if (i < 0) {
0N/A sign = '-';
0N/A i = -i;
0N/A }
0N/A
0N/A // Generate two digits per iteration
0N/A while (i >= 65536) {
0N/A q = i / 100;
0N/A // really: r = i - (q * 100);
0N/A r = i - ((q << 6) + (q << 5) + (q << 2));
0N/A i = q;
0N/A buf [--charPos] = DigitOnes[r];
0N/A buf [--charPos] = DigitTens[r];
0N/A }
0N/A
0N/A // Fall thru to fast mode for smaller numbers
0N/A // assert(i <= 65536, i);
0N/A for (;;) {
0N/A q = (i * 52429) >>> (16+3);
0N/A r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
0N/A buf [--charPos] = digits [r];
0N/A i = q;
0N/A if (i == 0) break;
0N/A }
0N/A if (sign != 0) {
0N/A buf [--charPos] = sign;
0N/A }
0N/A }
0N/A
0N/A final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
0N/A 99999999, 999999999, Integer.MAX_VALUE };
0N/A
0N/A // Requires positive x
0N/A static int stringSize(int x) {
0N/A for (int i=0; ; i++)
0N/A if (x <= sizeTable[i])
0N/A return i+1;
0N/A }
0N/A
0N/A /**
0N/A * Parses the string argument as a signed integer in the radix
0N/A * specified by the second argument. The characters in the string
0N/A * must all be digits of the specified radix (as determined by
0N/A * whether {@link java.lang.Character#digit(char, int)} returns a
0N/A * nonnegative value), except that the first character may be an
0N/A * ASCII minus sign {@code '-'} (<code>'&#92;u002D'</code>) to
0N/A * indicate a negative value or an ASCII plus sign {@code '+'}
0N/A * (<code>'&#92;u002B'</code>) to indicate a positive value. The
0N/A * resulting integer value is returned.
0N/A *
0N/A * <p>An exception of type {@code NumberFormatException} is
0N/A * thrown if any of the following situations occurs:
0N/A * <ul>
0N/A * <li>The first argument is {@code null} or is a string of
0N/A * length zero.
0N/A *
0N/A * <li>The radix is either smaller than
0N/A * {@link java.lang.Character#MIN_RADIX} or
0N/A * larger than {@link java.lang.Character#MAX_RADIX}.
0N/A *
0N/A * <li>Any character of the string is not a digit of the specified
0N/A * radix, except that the first character may be a minus sign
0N/A * {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
0N/A * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
0N/A * string is longer than length 1.
0N/A *
0N/A * <li>The value represented by the string is not a value of type
0N/A * {@code int}.
0N/A * </ul>
0N/A *
0N/A * <p>Examples:
0N/A * <blockquote><pre>
0N/A * parseInt("0", 10) returns 0
0N/A * parseInt("473", 10) returns 473
0N/A * parseInt("+42", 10) returns 42
0N/A * parseInt("-0", 10) returns 0
0N/A * parseInt("-FF", 16) returns -255
0N/A * parseInt("1100110", 2) returns 102
0N/A * parseInt("2147483647", 10) returns 2147483647
0N/A * parseInt("-2147483648", 10) returns -2147483648
0N/A * parseInt("2147483648", 10) throws a NumberFormatException
0N/A * parseInt("99", 8) throws a NumberFormatException
0N/A * parseInt("Kona", 10) throws a NumberFormatException
0N/A * parseInt("Kona", 27) returns 411787
0N/A * </pre></blockquote>
0N/A *
0N/A * @param s the {@code String} containing the integer
0N/A * representation to be parsed
0N/A * @param radix the radix to be used while parsing {@code s}.
0N/A * @return the integer represented by the string argument in the
0N/A * specified radix.
0N/A * @exception NumberFormatException if the {@code String}
0N/A * does not contain a parsable {@code int}.
0N/A */
0N/A public static int parseInt(String s, int radix)
0N/A throws NumberFormatException
0N/A {
1019N/A /*
1019N/A * WARNING: This method may be invoked early during VM initialization
1019N/A * before IntegerCache is initialized. Care must be taken to not use
1019N/A * the valueOf method.
1019N/A */
1019N/A
0N/A if (s == null) {
0N/A throw new NumberFormatException("null");
0N/A }
0N/A
0N/A if (radix < Character.MIN_RADIX) {
0N/A throw new NumberFormatException("radix " + radix +
0N/A " less than Character.MIN_RADIX");
0N/A }
0N/A
0N/A if (radix > Character.MAX_RADIX) {
0N/A throw new NumberFormatException("radix " + radix +
0N/A " greater than Character.MAX_RADIX");
0N/A }
0N/A
0N/A int result = 0;
0N/A boolean negative = false;
0N/A int i = 0, len = s.length();
0N/A int limit = -Integer.MAX_VALUE;
0N/A int multmin;
0N/A int digit;
0N/A
0N/A if (len > 0) {
0N/A char firstChar = s.charAt(0);
0N/A if (firstChar < '0') { // Possible leading "+" or "-"
0N/A if (firstChar == '-') {
0N/A negative = true;
0N/A limit = Integer.MIN_VALUE;
0N/A } else if (firstChar != '+')
0N/A throw NumberFormatException.forInputString(s);
0N/A
0N/A if (len == 1) // Cannot have lone "+" or "-"
0N/A throw NumberFormatException.forInputString(s);
0N/A i++;
0N/A }
0N/A multmin = limit / radix;
0N/A while (i < len) {
0N/A // Accumulating negatively avoids surprises near MAX_VALUE
0N/A digit = Character.digit(s.charAt(i++),radix);
0N/A if (digit < 0) {
0N/A throw NumberFormatException.forInputString(s);
0N/A }
0N/A if (result < multmin) {
0N/A throw NumberFormatException.forInputString(s);
0N/A }
0N/A result *= radix;
0N/A if (result < limit + digit) {
0N/A throw NumberFormatException.forInputString(s);
0N/A }
0N/A result -= digit;
0N/A }
0N/A } else {
0N/A throw NumberFormatException.forInputString(s);
0N/A }
0N/A return negative ? result : -result;
0N/A }
0N/A
0N/A /**
0N/A * Parses the string argument as a signed decimal integer. The
0N/A * characters in the string must all be decimal digits, except
0N/A * that the first character may be an ASCII minus sign {@code '-'}
0N/A * (<code>'&#92;u002D'</code>) to indicate a negative value or an
0N/A * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
0N/A * indicate a positive value. The resulting integer value is
0N/A * returned, exactly as if the argument and the radix 10 were
0N/A * given as arguments to the {@link #parseInt(java.lang.String,
0N/A * int)} method.
0N/A *
0N/A * @param s a {@code String} containing the {@code int}
0N/A * representation to be parsed
0N/A * @return the integer value represented by the argument in decimal.
0N/A * @exception NumberFormatException if the string does not contain a
0N/A * parsable integer.
0N/A */
0N/A public static int parseInt(String s) throws NumberFormatException {
0N/A return parseInt(s,10);
0N/A }
0N/A
0N/A /**
0N/A * Returns an {@code Integer} object holding the value
0N/A * extracted from the specified {@code String} when parsed
0N/A * with the radix given by the second argument. The first argument
0N/A * is interpreted as representing a signed integer in the radix
0N/A * specified by the second argument, exactly as if the arguments
0N/A * were given to the {@link #parseInt(java.lang.String, int)}
0N/A * method. The result is an {@code Integer} object that
0N/A * represents the integer value specified by the string.
0N/A *
0N/A * <p>In other words, this method returns an {@code Integer}
0N/A * object equal to the value of:
0N/A *
0N/A * <blockquote>
0N/A * {@code new Integer(Integer.parseInt(s, radix))}
0N/A * </blockquote>
0N/A *
0N/A * @param s the string to be parsed.
0N/A * @param radix the radix to be used in interpreting {@code s}
0N/A * @return an {@code Integer} object holding the value
0N/A * represented by the string argument in the specified
0N/A * radix.
0N/A * @exception NumberFormatException if the {@code String}
0N/A * does not contain a parsable {@code int}.
0N/A */
0N/A public static Integer valueOf(String s, int radix) throws NumberFormatException {
1019N/A return Integer.valueOf(parseInt(s,radix));
0N/A }
0N/A
0N/A /**
0N/A * Returns an {@code Integer} object holding the
0N/A * value of the specified {@code String}. The argument is
0N/A * interpreted as representing a signed decimal integer, exactly
0N/A * as if the argument were given to the {@link
0N/A * #parseInt(java.lang.String)} method. The result is an
0N/A * {@code Integer} object that represents the integer value
0N/A * specified by the string.
0N/A *
0N/A * <p>In other words, this method returns an {@code Integer}
0N/A * object equal to the value of:
0N/A *
0N/A * <blockquote>
0N/A * {@code new Integer(Integer.parseInt(s))}
0N/A * </blockquote>
0N/A *
0N/A * @param s the string to be parsed.
0N/A * @return an {@code Integer} object holding the value
0N/A * represented by the string argument.
0N/A * @exception NumberFormatException if the string cannot be parsed
0N/A * as an integer.
0N/A */
1019N/A public static Integer valueOf(String s) throws NumberFormatException {
1019N/A return Integer.valueOf(parseInt(s, 10));
1019N/A }
1019N/A
1019N/A /**
1019N/A * Cache to support the object identity semantics of autoboxing for values between
1019N/A * -128 and 127 (inclusive) as required by JLS.
1019N/A *
2916N/A * The cache is initialized on first usage. The size of the cache
2916N/A * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
2916N/A * During VM initialization, java.lang.Integer.IntegerCache.high property
2916N/A * may be set and saved in the private system properties in the
2916N/A * sun.misc.VM class.
1019N/A */
1019N/A
0N/A private static class IntegerCache {
1019N/A static final int low = -128;
1019N/A static final int high;
1019N/A static final Integer cache[];
0N/A
0N/A static {
1019N/A // high value may be configured by property
1019N/A int h = 127;
2916N/A String integerCacheHighPropValue =
2916N/A sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
1019N/A if (integerCacheHighPropValue != null) {
1019N/A int i = parseInt(integerCacheHighPropValue);
1019N/A i = Math.max(i, 127);
1019N/A // Maximum array size is Integer.MAX_VALUE
5863N/A h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
1019N/A }
1019N/A high = h;
1019N/A
1019N/A cache = new Integer[(high - low) + 1];
1019N/A int j = low;
1019N/A for(int k = 0; k < cache.length; k++)
1019N/A cache[k] = new Integer(j++);
0N/A }
1019N/A
1019N/A private IntegerCache() {}
0N/A }
0N/A
0N/A /**
0N/A * Returns an {@code Integer} instance representing the specified
0N/A * {@code int} value. If a new {@code Integer} instance is not
0N/A * required, this method should generally be used in preference to
0N/A * the constructor {@link #Integer(int)}, as this method is likely
0N/A * to yield significantly better space and time performance by
0N/A * caching frequently requested values.
0N/A *
1390N/A * This method will always cache values in the range -128 to 127,
1390N/A * inclusive, and may cache other values outside of this range.
1390N/A *
0N/A * @param i an {@code int} value.
0N/A * @return an {@code Integer} instance representing {@code i}.
0N/A * @since 1.5
0N/A */
0N/A public static Integer valueOf(int i) {
1019N/A assert IntegerCache.high >= 127;
1019N/A if (i >= IntegerCache.low && i <= IntegerCache.high)
1019N/A return IntegerCache.cache[i + (-IntegerCache.low)];
0N/A return new Integer(i);
0N/A }
0N/A
0N/A /**
0N/A * The value of the {@code Integer}.
0N/A *
0N/A * @serial
0N/A */
0N/A private final int value;
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Integer} object that
0N/A * represents the specified {@code int} value.
0N/A *
0N/A * @param value the value to be represented by the
0N/A * {@code Integer} object.
0N/A */
0N/A public Integer(int value) {
0N/A this.value = value;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Integer} object that
0N/A * represents the {@code int} value indicated by the
0N/A * {@code String} parameter. The string is converted to an
0N/A * {@code int} value in exactly the manner used by the
0N/A * {@code parseInt} method for radix 10.
0N/A *
0N/A * @param s the {@code String} to be converted to an
0N/A * {@code Integer}.
0N/A * @exception NumberFormatException if the {@code String} does not
0N/A * contain a parsable integer.
0N/A * @see java.lang.Integer#parseInt(java.lang.String, int)
0N/A */
0N/A public Integer(String s) throws NumberFormatException {
0N/A this.value = parseInt(s, 10);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Integer} as a
0N/A * {@code byte}.
0N/A */
0N/A public byte byteValue() {
0N/A return (byte)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Integer} as a
0N/A * {@code short}.
0N/A */
0N/A public short shortValue() {
0N/A return (short)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Integer} as an
0N/A * {@code int}.
0N/A */
0N/A public int intValue() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Integer} as a
0N/A * {@code long}.
0N/A */
0N/A public long longValue() {
0N/A return (long)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Integer} as a
0N/A * {@code float}.
0N/A */
0N/A public float floatValue() {
0N/A return (float)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Integer} as a
0N/A * {@code double}.
0N/A */
0N/A public double doubleValue() {
0N/A return (double)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code String} object representing this
0N/A * {@code Integer}'s value. The value is converted to signed
0N/A * decimal representation and returned as a string, exactly as if
0N/A * the integer value were given as an argument to the {@link
0N/A * java.lang.Integer#toString(int)} method.
0N/A *
0N/A * @return a string representation of the value of this object in
0N/A * base&nbsp;10.
0N/A */
0N/A public String toString() {
1722N/A return toString(value);
0N/A }
0N/A
0N/A /**
0N/A * Returns a hash code for this {@code Integer}.
0N/A *
0N/A * @return a hash code value for this object, equal to the
0N/A * primitive {@code int} value represented by this
0N/A * {@code Integer} object.
0N/A */
0N/A public int hashCode() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Compares this object to the specified object. The result is
0N/A * {@code true} if and only if the argument is not
0N/A * {@code null} and is an {@code Integer} object that
0N/A * contains the same {@code int} value as this object.
0N/A *
0N/A * @param obj the object to compare with.
0N/A * @return {@code true} if the objects are the same;
0N/A * {@code false} otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof Integer) {
0N/A return value == ((Integer)obj).intValue();
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Determines the integer value of the system property with the
0N/A * specified name.
0N/A *
0N/A * <p>The first argument is treated as the name of a system property.
0N/A * System properties are accessible through the
0N/A * {@link java.lang.System#getProperty(java.lang.String)} method. The
0N/A * string value of this property is then interpreted as an integer
0N/A * value and an {@code Integer} object representing this value is
0N/A * returned. Details of possible numeric formats can be found with
0N/A * the definition of {@code getProperty}.
0N/A *
0N/A * <p>If there is no property with the specified name, if the specified name
0N/A * is empty or {@code null}, or if the property does not have
0N/A * the correct numeric format, then {@code null} is returned.
0N/A *
0N/A * <p>In other words, this method returns an {@code Integer}
0N/A * object equal to the value of:
0N/A *
0N/A * <blockquote>
0N/A * {@code getInteger(nm, null)}
0N/A * </blockquote>
0N/A *
0N/A * @param nm property name.
0N/A * @return the {@code Integer} value of the property.
0N/A * @see java.lang.System#getProperty(java.lang.String)
0N/A * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
0N/A */
0N/A public static Integer getInteger(String nm) {
0N/A return getInteger(nm, null);
0N/A }
0N/A
0N/A /**
0N/A * Determines the integer value of the system property with the
0N/A * specified name.
0N/A *
0N/A * <p>The first argument is treated as the name of a system property.
0N/A * System properties are accessible through the {@link
0N/A * java.lang.System#getProperty(java.lang.String)} method. The
0N/A * string value of this property is then interpreted as an integer
0N/A * value and an {@code Integer} object representing this value is
0N/A * returned. Details of possible numeric formats can be found with
0N/A * the definition of {@code getProperty}.
0N/A *
0N/A * <p>The second argument is the default value. An {@code Integer} object
0N/A * that represents the value of the second argument is returned if there
0N/A * is no property of the specified name, if the property does not have
0N/A * the correct numeric format, or if the specified name is empty or
0N/A * {@code null}.
0N/A *
0N/A * <p>In other words, this method returns an {@code Integer} object
0N/A * equal to the value of:
0N/A *
0N/A * <blockquote>
0N/A * {@code getInteger(nm, new Integer(val))}
0N/A * </blockquote>
0N/A *
0N/A * but in practice it may be implemented in a manner such as:
0N/A *
0N/A * <blockquote><pre>
0N/A * Integer result = getInteger(nm, null);
0N/A * return (result == null) ? new Integer(val) : result;
0N/A * </pre></blockquote>
0N/A *
0N/A * to avoid the unnecessary allocation of an {@code Integer}
0N/A * object when the default value is not needed.
0N/A *
0N/A * @param nm property name.
0N/A * @param val default value.
0N/A * @return the {@code Integer} value of the property.
0N/A * @see java.lang.System#getProperty(java.lang.String)
0N/A * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
0N/A */
0N/A public static Integer getInteger(String nm, int val) {
0N/A Integer result = getInteger(nm, null);
1019N/A return (result == null) ? Integer.valueOf(val) : result;
0N/A }
0N/A
0N/A /**
0N/A * Returns the integer value of the system property with the
0N/A * specified name. The first argument is treated as the name of a
0N/A * system property. System properties are accessible through the
0N/A * {@link java.lang.System#getProperty(java.lang.String)} method.
0N/A * The string value of this property is then interpreted as an
0N/A * integer value, as per the {@code Integer.decode} method,
0N/A * and an {@code Integer} object representing this value is
0N/A * returned.
0N/A *
0N/A * <ul><li>If the property value begins with the two ASCII characters
0N/A * {@code 0x} or the ASCII character {@code #}, not
0N/A * followed by a minus sign, then the rest of it is parsed as a
0N/A * hexadecimal integer exactly as by the method
0N/A * {@link #valueOf(java.lang.String, int)} with radix 16.
0N/A * <li>If the property value begins with the ASCII character
0N/A * {@code 0} followed by another character, it is parsed as an
0N/A * octal integer exactly as by the method
0N/A * {@link #valueOf(java.lang.String, int)} with radix 8.
0N/A * <li>Otherwise, the property value is parsed as a decimal integer
0N/A * exactly as by the method {@link #valueOf(java.lang.String, int)}
0N/A * with radix 10.
0N/A * </ul>
0N/A *
0N/A * <p>The second argument is the default value. The default value is
0N/A * returned if there is no property of the specified name, if the
0N/A * property does not have the correct numeric format, or if the
0N/A * specified name is empty or {@code null}.
0N/A *
0N/A * @param nm property name.
0N/A * @param val default value.
0N/A * @return the {@code Integer} value of the property.
0N/A * @see java.lang.System#getProperty(java.lang.String)
0N/A * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
0N/A * @see java.lang.Integer#decode
0N/A */
0N/A public static Integer getInteger(String nm, Integer val) {
0N/A String v = null;
0N/A try {
0N/A v = System.getProperty(nm);
0N/A } catch (IllegalArgumentException e) {
0N/A } catch (NullPointerException e) {
0N/A }
0N/A if (v != null) {
0N/A try {
0N/A return Integer.decode(v);
0N/A } catch (NumberFormatException e) {
0N/A }
0N/A }
0N/A return val;
0N/A }
0N/A
0N/A /**
0N/A * Decodes a {@code String} into an {@code Integer}.
0N/A * Accepts decimal, hexadecimal, and octal numbers given
0N/A * by the following grammar:
0N/A *
0N/A * <blockquote>
0N/A * <dl>
0N/A * <dt><i>DecodableString:</i>
0N/A * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
0N/A * <p>
0N/A * <dt><i>Sign:</i>
0N/A * <dd>{@code -}
0N/A * <dd>{@code +}
0N/A * </dl>
0N/A * </blockquote>
0N/A *
0N/A * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
4008N/A * are as defined in section 3.10.1 of
4008N/A * <cite>The Java&trade; Language Specification</cite>,
4008N/A * except that underscores are not accepted between digits.
0N/A *
0N/A * <p>The sequence of characters following an optional
0N/A * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
0N/A * "{@code #}", or leading zero) is parsed as by the {@code
0N/A * Integer.parseInt} method with the indicated radix (10, 16, or
0N/A * 8). This sequence of characters must represent a positive
0N/A * value or a {@link NumberFormatException} will be thrown. The
0N/A * result is negated if first character of the specified {@code
0N/A * String} is the minus sign. No whitespace characters are
0N/A * permitted in the {@code String}.
0N/A *
0N/A * @param nm the {@code String} to decode.
0N/A * @return an {@code Integer} object holding the {@code int}
0N/A * value represented by {@code nm}
0N/A * @exception NumberFormatException if the {@code String} does not
0N/A * contain a parsable integer.
0N/A * @see java.lang.Integer#parseInt(java.lang.String, int)
0N/A */
0N/A public static Integer decode(String nm) throws NumberFormatException {
0N/A int radix = 10;
0N/A int index = 0;
0N/A boolean negative = false;
0N/A Integer result;
0N/A
0N/A if (nm.length() == 0)
0N/A throw new NumberFormatException("Zero length string");
0N/A char firstChar = nm.charAt(0);
0N/A // Handle sign, if present
0N/A if (firstChar == '-') {
0N/A negative = true;
0N/A index++;
0N/A } else if (firstChar == '+')
0N/A index++;
0N/A
0N/A // Handle radix specifier, if present
0N/A if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
0N/A index += 2;
0N/A radix = 16;
0N/A }
0N/A else if (nm.startsWith("#", index)) {
0N/A index ++;
0N/A radix = 16;
0N/A }
0N/A else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
0N/A index ++;
0N/A radix = 8;
0N/A }
0N/A
0N/A if (nm.startsWith("-", index) || nm.startsWith("+", index))
0N/A throw new NumberFormatException("Sign character in wrong position");
0N/A
0N/A try {
0N/A result = Integer.valueOf(nm.substring(index), radix);
1019N/A result = negative ? Integer.valueOf(-result.intValue()) : result;
0N/A } catch (NumberFormatException e) {
0N/A // If number is Integer.MIN_VALUE, we'll end up here. The next line
0N/A // handles this case, and causes any genuine format error to be
0N/A // rethrown.
0N/A String constant = negative ? ("-" + nm.substring(index))
0N/A : nm.substring(index);
0N/A result = Integer.valueOf(constant, radix);
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Compares two {@code Integer} objects numerically.
0N/A *
0N/A * @param anotherInteger the {@code Integer} to be compared.
0N/A * @return the value {@code 0} if this {@code Integer} is
0N/A * equal to the argument {@code Integer}; a value less than
0N/A * {@code 0} if this {@code Integer} is numerically less
0N/A * than the argument {@code Integer}; and a value greater
0N/A * than {@code 0} if this {@code Integer} is numerically
0N/A * greater than the argument {@code Integer} (signed
0N/A * comparison).
0N/A * @since 1.2
0N/A */
0N/A public int compareTo(Integer anotherInteger) {
1701N/A return compare(this.value, anotherInteger.value);
1701N/A }
1701N/A
1701N/A /**
1701N/A * Compares two {@code int} values numerically.
1701N/A * The value returned is identical to what would be returned by:
1701N/A * <pre>
1701N/A * Integer.valueOf(x).compareTo(Integer.valueOf(y))
1701N/A * </pre>
1701N/A *
1701N/A * @param x the first {@code int} to compare
1701N/A * @param y the second {@code int} to compare
1701N/A * @return the value {@code 0} if {@code x == y};
1701N/A * a value less than {@code 0} if {@code x < y}; and
1701N/A * a value greater than {@code 0} if {@code x > y}
1701N/A * @since 1.7
1701N/A */
1701N/A public static int compare(int x, int y) {
1701N/A return (x < y) ? -1 : ((x == y) ? 0 : 1);
0N/A }
0N/A
0N/A
0N/A // Bit twiddling
0N/A
0N/A /**
0N/A * The number of bits used to represent an {@code int} value in two's
0N/A * complement binary form.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public static final int SIZE = 32;
0N/A
0N/A /**
0N/A * Returns an {@code int} value with at most a single one-bit, in the
0N/A * position of the highest-order ("leftmost") one-bit in the specified
0N/A * {@code int} value. Returns zero if the specified value has no
0N/A * one-bits in its two's complement binary representation, that is, if it
0N/A * is equal to zero.
0N/A *
0N/A * @return an {@code int} value with a single one-bit, in the position
0N/A * of the highest-order one-bit in the specified value, or zero if
0N/A * the specified value is itself equal to zero.
0N/A * @since 1.5
0N/A */
0N/A public static int highestOneBit(int i) {
0N/A // HD, Figure 3-1
0N/A i |= (i >> 1);
0N/A i |= (i >> 2);
0N/A i |= (i >> 4);
0N/A i |= (i >> 8);
0N/A i |= (i >> 16);
0N/A return i - (i >>> 1);
0N/A }
0N/A
0N/A /**
0N/A * Returns an {@code int} value with at most a single one-bit, in the
0N/A * position of the lowest-order ("rightmost") one-bit in the specified
0N/A * {@code int} value. Returns zero if the specified value has no
0N/A * one-bits in its two's complement binary representation, that is, if it
0N/A * is equal to zero.
0N/A *
0N/A * @return an {@code int} value with a single one-bit, in the position
0N/A * of the lowest-order one-bit in the specified value, or zero if
0N/A * the specified value is itself equal to zero.
0N/A * @since 1.5
0N/A */
0N/A public static int lowestOneBit(int i) {
0N/A // HD, Section 2-1
0N/A return i & -i;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of zero bits preceding the highest-order
0N/A * ("leftmost") one-bit in the two's complement binary representation
0N/A * of the specified {@code int} value. Returns 32 if the
0N/A * specified value has no one-bits in its two's complement representation,
0N/A * in other words if it is equal to zero.
0N/A *
0N/A * <p>Note that this method is closely related to the logarithm base 2.
0N/A * For all positive {@code int} values x:
0N/A * <ul>
0N/A * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
0N/A * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
0N/A * </ul>
0N/A *
0N/A * @return the number of zero bits preceding the highest-order
0N/A * ("leftmost") one-bit in the two's complement binary representation
0N/A * of the specified {@code int} value, or 32 if the value
0N/A * is equal to zero.
0N/A * @since 1.5
0N/A */
0N/A public static int numberOfLeadingZeros(int i) {
0N/A // HD, Figure 5-6
0N/A if (i == 0)
0N/A return 32;
0N/A int n = 1;
0N/A if (i >>> 16 == 0) { n += 16; i <<= 16; }
0N/A if (i >>> 24 == 0) { n += 8; i <<= 8; }
0N/A if (i >>> 28 == 0) { n += 4; i <<= 4; }
0N/A if (i >>> 30 == 0) { n += 2; i <<= 2; }
0N/A n -= i >>> 31;
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of zero bits following the lowest-order ("rightmost")
0N/A * one-bit in the two's complement binary representation of the specified
0N/A * {@code int} value. Returns 32 if the specified value has no
0N/A * one-bits in its two's complement representation, in other words if it is
0N/A * equal to zero.
0N/A *
0N/A * @return the number of zero bits following the lowest-order ("rightmost")
0N/A * one-bit in the two's complement binary representation of the
0N/A * specified {@code int} value, or 32 if the value is equal
0N/A * to zero.
0N/A * @since 1.5
0N/A */
0N/A public static int numberOfTrailingZeros(int i) {
0N/A // HD, Figure 5-14
0N/A int y;
0N/A if (i == 0) return 32;
0N/A int n = 31;
0N/A y = i <<16; if (y != 0) { n = n -16; i = y; }
0N/A y = i << 8; if (y != 0) { n = n - 8; i = y; }
0N/A y = i << 4; if (y != 0) { n = n - 4; i = y; }
0N/A y = i << 2; if (y != 0) { n = n - 2; i = y; }
0N/A return n - ((i << 1) >>> 31);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of one-bits in the two's complement binary
0N/A * representation of the specified {@code int} value. This function is
0N/A * sometimes referred to as the <i>population count</i>.
0N/A *
0N/A * @return the number of one-bits in the two's complement binary
0N/A * representation of the specified {@code int} value.
0N/A * @since 1.5
0N/A */
0N/A public static int bitCount(int i) {
0N/A // HD, Figure 5-2
0N/A i = i - ((i >>> 1) & 0x55555555);
0N/A i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
0N/A i = (i + (i >>> 4)) & 0x0f0f0f0f;
0N/A i = i + (i >>> 8);
0N/A i = i + (i >>> 16);
0N/A return i & 0x3f;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value obtained by rotating the two's complement binary
0N/A * representation of the specified {@code int} value left by the
0N/A * specified number of bits. (Bits shifted out of the left hand, or
0N/A * high-order, side reenter on the right, or low-order.)
0N/A *
0N/A * <p>Note that left rotation with a negative distance is equivalent to
0N/A * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
0N/A * distance)}. Note also that rotation by any multiple of 32 is a
0N/A * no-op, so all but the last five bits of the rotation distance can be
0N/A * ignored, even if the distance is negative: {@code rotateLeft(val,
0N/A * distance) == rotateLeft(val, distance & 0x1F)}.
0N/A *
0N/A * @return the value obtained by rotating the two's complement binary
0N/A * representation of the specified {@code int} value left by the
0N/A * specified number of bits.
0N/A * @since 1.5
0N/A */
0N/A public static int rotateLeft(int i, int distance) {
0N/A return (i << distance) | (i >>> -distance);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value obtained by rotating the two's complement binary
0N/A * representation of the specified {@code int} value right by the
0N/A * specified number of bits. (Bits shifted out of the right hand, or
0N/A * low-order, side reenter on the left, or high-order.)
0N/A *
0N/A * <p>Note that right rotation with a negative distance is equivalent to
0N/A * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
0N/A * distance)}. Note also that rotation by any multiple of 32 is a
0N/A * no-op, so all but the last five bits of the rotation distance can be
0N/A * ignored, even if the distance is negative: {@code rotateRight(val,
0N/A * distance) == rotateRight(val, distance & 0x1F)}.
0N/A *
0N/A * @return the value obtained by rotating the two's complement binary
0N/A * representation of the specified {@code int} value right by the
0N/A * specified number of bits.
0N/A * @since 1.5
0N/A */
0N/A public static int rotateRight(int i, int distance) {
0N/A return (i >>> distance) | (i << -distance);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value obtained by reversing the order of the bits in the
0N/A * two's complement binary representation of the specified {@code int}
0N/A * value.
0N/A *
0N/A * @return the value obtained by reversing order of the bits in the
0N/A * specified {@code int} value.
0N/A * @since 1.5
0N/A */
0N/A public static int reverse(int i) {
0N/A // HD, Figure 7-1
0N/A i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
0N/A i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
0N/A i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
0N/A i = (i << 24) | ((i & 0xff00) << 8) |
0N/A ((i >>> 8) & 0xff00) | (i >>> 24);
0N/A return i;
0N/A }
0N/A
0N/A /**
0N/A * Returns the signum function of the specified {@code int} value. (The
0N/A * return value is -1 if the specified value is negative; 0 if the
0N/A * specified value is zero; and 1 if the specified value is positive.)
0N/A *
0N/A * @return the signum function of the specified {@code int} value.
0N/A * @since 1.5
0N/A */
0N/A public static int signum(int i) {
0N/A // HD, Section 2-7
0N/A return (i >> 31) | (-i >>> 31);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value obtained by reversing the order of the bytes in the
0N/A * two's complement representation of the specified {@code int} value.
0N/A *
0N/A * @return the value obtained by reversing the bytes in the specified
0N/A * {@code int} value.
0N/A * @since 1.5
0N/A */
0N/A public static int reverseBytes(int i) {
0N/A return ((i >>> 24) ) |
0N/A ((i >> 8) & 0xFF00) |
0N/A ((i << 8) & 0xFF0000) |
0N/A ((i << 24));
0N/A }
0N/A
0N/A /** use serialVersionUID from JDK 1.0.2 for interoperability */
0N/A private static final long serialVersionUID = 1360826667806852920L;
0N/A}