public final class IntegerUtil
extends java.lang.Object
int
.
As with the specification, this implementation relies on code laid out in Henry
S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002) as well as The
Aggregate's Magic Algorithms.Number
Modifier and Type | Field and Description |
---|---|
static int |
MAX_RADIX
The maximum radix used for conversions between characters and integers.
|
static int |
MAX_VALUE
Constant for the maximum
int value, 231-1. |
static int |
MIN_RADIX
The minimum radix used for conversions between characters and integers.
|
static int |
MIN_VALUE
Constant for the minimum
int value, -231. |
Constructor and Description |
---|
IntegerUtil() |
Modifier and Type | Method and Description |
---|---|
static int |
bitCount(int i)
Counts the number of 1 bits in the specified integer; this is also referred to as population count.
|
static int |
decode(java.lang.String string)
Parses the specified string and returns an
int if the string can be decoded into an integer
value. |
static int |
digit(char c,
int radix)
Determine the value of the specified character
c in the supplied radix. |
static int |
highestOneBit(int i)
Determines the highest (leftmost) bit of the specified integer that is 1 and returns the bit mask value for that
bit.
|
static int |
lowestOneBit(int i)
Determines the lowest (rightmost) bit of the specified integer that is 1 and returns the bit mask value for that
bit.
|
static int |
numberOfLeadingZeros(int i)
Determines the number of leading zeros in the specified integer prior to the
highest
one bit . |
static int |
numberOfTrailingZeros(int i)
Determines the number of trailing zeros in the specified integer after the
lowest one
bit . |
static int |
parseInt(java.lang.String string)
Parses the specified string as a signed decimal integer value.
|
static int |
parseInt(java.lang.String string,
int radix)
Parses the specified string as a signed integer value using the specified radix.
|
static int |
reverse(int i)
Reverses the order of the bits of the specified integer.
|
static int |
reverseBytes(int i)
Reverses the order of the bytes of the specified integer.
|
static int |
rotateLeft(int i,
int distance)
Rotates the bits of the specified integer to the left by the specified number of bits.
|
static int |
rotateRight(int i,
int distance)
Rotates the bits of the specified integer to the right by the specified number of bits.
|
static java.lang.String |
toBinaryString(int i)
Converts the specified integer into its binary string representation.
|
static java.lang.String |
toDecimalString(int value) |
static java.lang.String |
toHexString(int i)
Converts the specified integer into its hexadecimal string representation.
|
static java.lang.String |
toString(int value)
Converts the specified integer into its decimal string representation.
|
public static final int MAX_VALUE
int
value, 231-1.public static final int MIN_VALUE
int
value, -231.public static final int MIN_RADIX
public static final int MAX_RADIX
public static int decode(java.lang.String string) throws InvalidFormatException
int
if the string can be decoded into an integer
value. The string may be an optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal
("0..."), or decimal ("...") representation of an integer.string
- a string representation of an integer value.string
InvalidFormatException
- if string
can not be parsed as an integer valuepublic static int parseInt(java.lang.String string) throws InvalidFormatException
string
- the string representation of an integer value.string
.InvalidFormatException
- if string
cannot be parsed as an integer value.public static int parseInt(java.lang.String string, int radix) throws InvalidFormatException
string
- the string representation of an integer value.radix
- the radix to use when parsing.string
using radix
.InvalidFormatException
- if string
is null
or has a length of zero, radix <
Character.MIN_RADIX
, radix > Character.MAX_RADIX
, or if string
can not be parsed as an integer value.public static java.lang.String toBinaryString(int i)
i
- the integer to convert.i
.public static java.lang.String toHexString(int i)
i
- the integer to convert.i
.public static java.lang.String toDecimalString(int value)
public static java.lang.String toString(int value)
value
- the integer to convert.value
.public static int highestOneBit(int i)
i
- the integer to examine.i
.public static int lowestOneBit(int i)
i
- the integer to examine.i
.public static int numberOfLeadingZeros(int i)
highest
one bit
.i
- the integer to examine.i
.public static int numberOfTrailingZeros(int i)
lowest one
bit
.i
- the integer to examine.i
.public static int bitCount(int i)
i
- the integer to examine.i
.public static int rotateLeft(int i, int distance)
i
- the integer value to rotate left.distance
- the number of bits to rotate.public static int rotateRight(int i, int distance)
i
- the integer value to rotate right.distance
- the number of bits to rotate.public static int reverseBytes(int i)
i
- the integer value for which to reverse the byte order.public static int reverse(int i)
i
- the integer value for which to reverse the bit order.public static int digit(char c, int radix)
c
in the supplied radix. The value of radix
must
be between 2 and 36. If the character isn't a digit/letter or the value is outside the specified radix (e.g.
it's an 'f' when the radix is 10) then an InvalidFormatException is thrown. Only ASCII characters (that is,
Arabic number characters 0-9 and letters a-z / A-Z) are supported.c
- the character to determine the value of.radix
- the radix.c
in radix
if radix
lies between MIN_RADIX
and MAX_RADIX
InvalidFormatException
- if character isn't digit/letter or returned value would be outside specified
radix