public final class Fast extends Object
This class contains static optimised utility methods that are used by all classes manipulating bits. They include:
log2(double)
, ceilLog2(int)
, etc.;
Modifier and Type | Field and Description |
---|---|
static int[] |
BYTELSB
Precomputed least significant bits for bytes (-1 for 0 ).
|
static int[] |
BYTEMSB
Precomputed most significant bits for bytes (-1 for 0 ).
|
static long |
MSBS_STEP_8 |
static long |
ONES_STEP_4 |
static long |
ONES_STEP_8 |
Modifier and Type | Method and Description |
---|---|
static int |
ceilLog2(int x)
Computes the ceiling of the base-two logarithm of the argument.
|
static int |
ceilLog2(long x)
Computes the ceiling of the base-two logarithm of the argument.
|
static int |
count(long x)
Returns the number of bits set to one in a long.
|
static int |
int2nat(int x)
Maps integers bijectively into natural numbers.
|
static long |
int2nat(long x)
Maps longs bijectively into long natural numbers.
|
static int |
leastSignificantBit(long x)
Computes the least significant bit of a long integer.
|
static int |
length(int x)
Returns the number of bits that are necessary to encode the argument.
|
static int |
length(long x)
Returns the number of bits that are necessary to encode the argument.
|
static double |
log2(double x)
Returns the base-two logarithm of the argument.
|
static void |
main(String[] a) |
static int |
mostSignificantBit(int x)
Returns the most significant bit of an integer.
|
static int |
mostSignificantBit(long x)
Returns the most significant bit of a long.
|
static int |
nat2int(int x)
Maps natural numbers bijectively into integers.
|
static long |
nat2int(long x)
Maps long natural numbers bijectively into longs.
|
static int |
select(long x,
int rank)
Returns the position of a bit of given rank.
|
public static final long ONES_STEP_4
public static final long ONES_STEP_8
public static final long MSBS_STEP_8
public static final int[] BYTELSB
public static final int[] BYTEMSB
public static int int2nat(int x)
This method will map a negative integer x to -2x-1 and
a nonnegative integer x to 2x. It can be used to save
integers in the range [Integer.MIN_VALUE
/2..Integer.MAX_VALUE
/2]
(i.e., [-230..230-1])
using the standard coding methods (which all work on natural numbers). Note
that no range checks are performed.
The inverse of the above map is computed by nat2int(int)
.
x
- an integer.nat2int(int)
public static int nat2int(int x)
This method computes the inverse of int2nat(int)
.
x
- a natural number.int2nat(int)
public static long int2nat(long x)
This method will map a negative long x to -2x-1 and
a nonnegative long x to 2x. It can be used to save
longs in the range [Long.MIN_VALUE
/2..Long.MAX_VALUE
/2]
(i.e., [-262..262-1])
using the standard coding methods (which all work on natural numbers). Note
that no range checks are performed.
The inverse of the above map is computed by nat2int(long)
.
x
- a long.int2nat(int)
public static long nat2int(long x)
This method computes the inverse of int2nat(long)
.
x
- a long natural number.nat2int(int)
public static double log2(double x)
x
- a double.x
.public static int ceilLog2(int x)
This method relies of mostSignificantBit(int)
, and thus is pretty fast.
x
- an integer.x
is zero.public static int ceilLog2(long x)
This method relies of mostSignificantBit(long)
, and thus is pretty fast.
x
- an integer.x
is zero.public static int length(int x)
x
- an integer.x
.public static int length(long x)
x
- a long.x
.public static int count(long x)
This method implements a classical broadword algorithm.
x
- a long.x
.public static int select(long x, int rank)
This method implements a new broadword algorithm.
x
- a long.rank
- a rank (smaller than 128).x
of the bit of rank k
ones; if no such
bit exists, returns 72.public static int mostSignificantBit(long x)
This method implements Gerth Brodal's broadword algorithm. On 64-bit architectures it is an order of magnitude faster than standard bit-fiddling techniques.
x
- a long.x
, of x
is nonzero; −1, otherwise.public static int mostSignificantBit(int x)
x
- an integer.x
, of x
is nonzero; −1, otherwise.mostSignificantBit(long)
public static int leastSignificantBit(long x)
x
- a long integer.public static void main(String[] a)
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.