/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package java.lang; /** * The wrapper for the primitive type {@code int}. *
* Implementation note: The "bit twiddling" methods in this class use techniques
* described in Henry S. Warren,
* Jr.'s Hacker's Delight, (Addison Wesley, 2002) and Sean Anderson's
* Bit Twiddling Hacks.
*
* @see java.lang.Long
* @since 1.0
*/
@FindBugsSuppressWarnings("DM_NUMBER_CTOR")
public final class Integer extends Number implements Comparable This method treats its argument as signed. If you want to convert an
* unsigned value to one of the common non-decimal bases, you may find
* {@link #toBinaryString}, {@code #toHexString}, or {@link #toOctalString}
* more convenient.
*
* @param i
* the signed integer to convert.
* @param radix
* the base to use for the conversion.
* @return the string representation of {@code i}.
*/
public static String toString(int i, int radix) {
return IntegralToString.intToString(i, radix);
}
/**
* Parses the specified string as a signed decimal integer value.
*
* @param string
* the string representation of an integer value.
* @return an {@code Integer} instance containing the integer value
* represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
* @see #parseInt(String)
*/
public static Integer valueOf(String string) throws NumberFormatException {
return valueOf(parseInt(string));
}
/**
* Parses the specified string as a signed integer value using the specified
* radix.
*
* @param string
* the string representation of an integer value.
* @param radix
* the radix to use when parsing.
* @return an {@code Integer} instance containing the integer value
* represented by {@code string} using {@code radix}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
* @see #parseInt(String, int)
*/
public static Integer valueOf(String string, int radix) throws NumberFormatException {
return valueOf(parseInt(string, radix));
}
/**
* Determines the highest (leftmost) bit of the specified integer that is 1
* and returns the bit mask value for that bit. This is also referred to as
* the Most Significant 1 Bit. Returns zero if the specified integer is
* zero.
*
* @param i
* the integer to examine.
* @return the bit mask indicating the highest 1 bit in {@code i}.
* @since 1.5
*/
public static int highestOneBit(int i) {
// Hacker's Delight, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >>> 1);
}
/**
* Determines the lowest (rightmost) bit of the specified integer that is 1
* and returns the bit mask value for that bit. This is also referred
* to as the Least Significant 1 Bit. Returns zero if the specified integer
* is zero.
*
* @param i
* the integer to examine.
* @return the bit mask indicating the lowest 1 bit in {@code i}.
* @since 1.5
*/
public static int lowestOneBit(int i) {
return i & -i;
}
/**
* Determines the number of leading zeros in the specified integer prior to
* the {@link #highestOneBit(int) highest one bit}.
*
* @param i
* the integer to examine.
* @return the number of leading zeros in {@code i}.
* @since 1.5
*/
public static int numberOfLeadingZeros(int i) {
// Hacker's Delight, Figure 5-6
if (i <= 0) {
return (~i >> 26) & 32;
}
int n = 1;
if (i >> 16 == 0) {
n += 16;
i <<= 16;
}
if (i >> 24 == 0) {
n += 8;
i <<= 8;
}
if (i >> 28 == 0) {
n += 4;
i <<= 4;
}
if (i >> 30 == 0) {
n += 2;
i <<= 2;
}
return n - (i >>> 31);
}
/**
* Determines the number of trailing zeros in the specified integer after
* the {@link #lowestOneBit(int) lowest one bit}.
*
* @param i
* the integer to examine.
* @return the number of trailing zeros in {@code i}.
* @since 1.5
*/
public static int numberOfTrailingZeros(int i) {
return NTZ_TABLE[((i & -i) * 0x0450FBAF) >>> 26];
}
/**
* Counts the number of 1 bits in the specified integer; this is also
* referred to as population count.
*
* @param i
* the integer to examine.
* @return the number of 1 bits in {@code i}.
* @since 1.5
*/
public static int bitCount(int i) {
// Hacker's Delight, Figure 5-2
i -= (i >> 1) & 0x55555555;
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = ((i >> 4) + i) & 0x0F0F0F0F;
i += i >> 8;
i += i >> 16;
return i & 0x0000003F;
}
/**
* Rotates the bits of the specified integer to the left by the specified
* number of bits.
*
* @param i
* the integer value to rotate left.
* @param distance
* the number of bits to rotate.
* @return the rotated value.
* @since 1.5
*/
public static int rotateLeft(int i, int distance) {
// Shift distances are mod 32 (JLS3 15.19), so we needn't mask -distance
return (i << distance) | (i >>> -distance);
}
/**
* Rotates the bits of the specified integer to the right by the specified
* number of bits.
*
* @param i
* the integer value to rotate right.
* @param distance
* the number of bits to rotate.
* @return the rotated value.
* @since 1.5
*/
public static int rotateRight(int i, int distance) {
// Shift distances are mod 32 (JLS3 15.19), so we needn't mask -distance
return (i >>> distance) | (i << -distance);
}
/**
* Reverses the order of the bytes of the specified integer.
*
* @param i
* the integer value for which to reverse the byte order.
* @return the reversed value.
* @since 1.5
*/
public static int reverseBytes(int i) {
// Hacker's Delight 7-1, with minor tweak from Veldmeijer
// http://graphics.stanford.edu/~seander/bithacks.html
i = ((i >>> 8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8);
return ( i >>> 16 ) | ( i << 16);
}
/**
* Reverses the order of the bits of the specified integer.
*
* @param i
* the integer value for which to reverse the bit order.
* @return the reversed value.
* @since 1.5
*/
public static int reverse(int i) {
// Hacker's Delight 7-1, with minor tweak from Veldmeijer
// http://graphics.stanford.edu/~seander/bithacks.html
i = ((i >>> 1) & 0x55555555) | ((i & 0x55555555) << 1);
i = ((i >>> 2) & 0x33333333) | ((i & 0x33333333) << 2);
i = ((i >>> 4) & 0x0F0F0F0F) | ((i & 0x0F0F0F0F) << 4);
i = ((i >>> 8) & 0x00FF00FF) | ((i & 0x00FF00FF) << 8);
return ((i >>> 16) ) | ((i ) << 16);
}
/**
* Returns the value of the {@code signum} function for the specified
* integer.
*
* @param i
* the integer value to check.
* @return -1 if {@code i} is negative, 1 if {@code i} is positive, 0 if
* {@code i} is zero.
* @since 1.5
*/
public static int signum(int i) {
return (i >> 31) | (-i >>> 31); // Hacker's delight 2-7
}
/**
* Returns a {@code Integer} instance for the specified integer value.
*
* If it is not necessary to get a new {@code Integer} instance, it is
* recommended to use this method instead of the constructor, since it
* maintains a cache of instances which may result in better performance.
*
* @param i
* the integer value to store in the instance.
* @return a {@code Integer} instance containing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
return i >= 128 || i < -128 ? new Integer(i) : SMALL_VALUES[i + 128];
}
/**
* A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing
*/
private static final Integer[] SMALL_VALUES = new Integer[256];
static {
for (int i = -128; i < 128; i++) {
SMALL_VALUES[i + 128] = new Integer(i);
}
}
}