public final class LongUtil
extends java.lang.Object
long
.
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 long |
MAX_VALUE
Constant for the maximum
long value, 263-1. |
static long |
MIN_VALUE
Constant for the minimum
long value, -263. |
Constructor and Description |
---|
LongUtil() |
Modifier and Type | Method and Description |
---|---|
static int |
bitCount(long lng)
Counts the number of 1 bits in the specified long value; this is also referred to as population count.
|
static long |
decode(java.lang.String string)
Parses the specified string and returns a
long if the string can be decoded into a long value. |
static long |
highestOneBit(long lng)
Determines the highest (leftmost) bit of the specified long value that is 1 and returns the bit mask value for
that bit.
|
static long |
lowestOneBit(long lng)
Determines the lowest (rightmost) bit of the specified long value that is 1 and returns the bit mask value for
that bit.
|
static int |
numberOfLeadingZeros(long lng)
Determines the number of leading zeros in the specified long value prior to the
highest one bit . |
static int |
numberOfTrailingZeros(long lng)
Determines the number of trailing zeros in the specified long value after the
lowest
one bit . |
static long |
parseLong(java.lang.String string)
Parses the specified string as a signed decimal long value.
|
static long |
parseLong(java.lang.String string,
int radix)
Parses the specified string as a signed long value using the specified radix.
|
static long |
reverse(long lng)
Reverses the order of the bits of the specified long value.
|
static long |
reverseBytes(long lng)
Reverses the order of the bytes of the specified long value.
|
static long |
rotateLeft(long lng,
int distance)
Rotates the bits of the specified long value to the left by the specified number of bits.
|
static long |
rotateRight(long lng,
int distance)
Rotates the bits of the specified long value to the right by the specified number of bits.
|
static java.lang.String |
toBinaryString(long l)
Converts the specified long value into its binary string representation.
|
static java.lang.String |
toHexString(long l)
Converts the specified long value into its hexadecimal string representation.
|
static java.lang.String |
toString(long l)
Converts the specified long value into its decimal string representation.
|
public static final long MAX_VALUE
long
value, 263-1.public static final long MIN_VALUE
long
value, -263.public static long decode(java.lang.String string) throws InvalidFormatException
long
if the string can be decoded into a long value.
The string may be an optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."), octal ("0..."), or
decimal ("...") representation of a long.string
- a string representation of a long valuelong
for the value represented by string
InvalidFormatException
- if string
can not be parsed as a long valuepublic static long parseLong(java.lang.String string) throws InvalidFormatException
string
- the string representation of a long value.string
.InvalidFormatException
- if string
is null
, has a length of zero or can not be parsed as a
long value.public static long parseLong(java.lang.String string, int radix) throws InvalidFormatException
string
- the string representation of a long 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 a long value.public static java.lang.String toBinaryString(long l)
l
- the long value to convert.l
.public static java.lang.String toHexString(long l)
l
- the long value to convert.l
.public static java.lang.String toString(long l)
l
- the long to convert.l
.public static long highestOneBit(long lng)
lng
- the long to examine.lng
.public static long lowestOneBit(long lng)
lng
- the long to examine.lng
.public static int numberOfLeadingZeros(long lng)
highest one bit
.lng
- the long to examine.lng
.public static int numberOfTrailingZeros(long lng)
lowest
one bit
.lng
- the long to examine.lng
.public static int bitCount(long lng)
lng
- the long to examine.lng
.public static long rotateLeft(long lng, int distance)
lng
- the long value to rotate left.distance
- the number of bits to rotate.public static long rotateRight(long lng, int distance)
lng
- the long value to rotate right.distance
- the number of bits to rotate.public static long reverseBytes(long lng)
lng
- the long value for which to reverse the byte order.public static long reverse(long lng)
lng
- the long value for which to reverse the bit order.