jsimple.util

## Class IntegerUtil

• java.lang.Object
• jsimple.util.IntegerUtil
• ### Field Summary

Fields
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 Summary

Constructors
Constructor and Description
`IntegerUtil()`
• ### Method Summary

All Methods
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.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### MAX_VALUE

`public static final int MAX_VALUE`
Constant for the maximum `int` value, 231-1.
Constant Field Values
• #### MIN_VALUE

`public static final int MIN_VALUE`
Constant for the minimum `int` value, -231.
Constant Field Values

`public static final int MIN_RADIX`
The minimum radix used for conversions between characters and integers.
Constant Field Values

`public static final int MAX_RADIX`
The maximum radix used for conversions between characters and integers.
Constant Field Values
• ### Constructor Detail

• #### IntegerUtil

`public IntegerUtil()`
• ### Method Detail

• #### decode

```public static int decode(java.lang.String string)
throws InvalidFormatException```
Parses the specified string and returns an `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.
Parameters:
`string` - a string representation of an integer value.
Returns:
an int for the value represented by `string`
Throws:
`InvalidFormatException` - if `string` can not be parsed as an integer value
• #### parseInt

```public static int parseInt(java.lang.String string)
throws InvalidFormatException```
Parses the specified string as a signed decimal integer value. The ASCII character - ('-') is recognized as the minus sign.
Parameters:
`string` - the string representation of an integer value.
Returns:
the primitive integer value represented by `string`.
Throws:
`InvalidFormatException` - if `string` cannot be parsed as an integer value.
• #### parseInt

```public static int parseInt(java.lang.String string,
throws InvalidFormatException```
Parses the specified string as a signed integer value using the specified radix. The ASCII character - ('-') is recognized as the minus sign.
Parameters:
`string` - the string representation of an integer value.
`radix` - the radix to use when parsing.
Returns:
the primitive integer value represented by `string` using `radix`.
Throws:
`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.
• #### toBinaryString

`public static java.lang.String toBinaryString(int i)`
Converts the specified integer into its binary string representation. The returned string is a concatenation of '0' and '1' characters.
Parameters:
`i` - the integer to convert.
Returns:
the binary string representation of `i`.
• #### toHexString

`public static java.lang.String toHexString(int i)`
Converts the specified integer into its hexadecimal string representation. The returned string is a concatenation of characters from '0' to '9' and 'a' to 'f'.
Parameters:
`i` - the integer to convert.
Returns:
the hexadecimal string representation of `i`.
• #### toDecimalString

`public static java.lang.String toDecimalString(int value)`
• #### toString

`public static java.lang.String toString(int value)`
Converts the specified integer into its decimal string representation. The returned string is a concatenation of a minus sign if the number is negative and characters from '0' to '9'.
Parameters:
`value` - the integer to convert.
Returns:
the decimal string representation of `value`.
• #### highestOneBit

`public 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. This is also referred to as the Most Significant 1 Bit. Returns zero if the specified integer is zero.
Parameters:
`i` - the integer to examine.
Returns:
the bit mask indicating the highest 1 bit in `i`.
Since:
1.5
• #### lowestOneBit

`public 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. This is also referred to as the Least Significant 1 Bit. Returns zero if the specified integer is zero.
Parameters:
`i` - the integer to examine.
Returns:
the bit mask indicating the lowest 1 bit in `i`.
Since:
1.5

`public static int numberOfLeadingZeros(int i)`
Determines the number of leading zeros in the specified integer prior to the ```highest one bit```.
Parameters:
`i` - the integer to examine.
Returns:
the number of leading zeros in `i`.
Since:
1.5
• #### numberOfTrailingZeros

`public static int numberOfTrailingZeros(int i)`
Determines the number of trailing zeros in the specified integer after the ```lowest one bit```.
Parameters:
`i` - the integer to examine.
Returns:
the number of trailing zeros in `i`.
Since:
1.5
• #### bitCount

`public static int bitCount(int i)`
Counts the number of 1 bits in the specified integer; this is also referred to as population count.
Parameters:
`i` - the integer to examine.
Returns:
the number of 1 bits in `i`.
Since:
1.5
• #### rotateLeft

```public static int rotateLeft(int i,
int distance)```
Rotates the bits of the specified integer to the left by the specified number of bits.
Parameters:
`i` - the integer value to rotate left.
`distance` - the number of bits to rotate.
Returns:
the rotated value.
Since:
1.5
• #### rotateRight

```public static int rotateRight(int i,
int distance)```
Rotates the bits of the specified integer to the right by the specified number of bits.
Parameters:
`i` - the integer value to rotate right.
`distance` - the number of bits to rotate.
Returns:
the rotated value.
Since:
1.5
• #### reverseBytes

`public static int reverseBytes(int i)`
Reverses the order of the bytes of the specified integer.
Parameters:
`i` - the integer value for which to reverse the byte order.
Returns:
the reversed value.
Since:
1.5
• #### reverse

`public static int reverse(int i)`
Reverses the order of the bits of the specified integer.
Parameters:
`i` - the integer value for which to reverse the bit order.
Returns:
the reversed value.
Since:
1.5
• #### digit

```public static int digit(char c,
Determine the value of the specified character `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.
the value of `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