public final class MathUtils extends Object
Modifier and Type | Method and Description |
---|---|
static int |
bitMix(int in)
Bit-mixing for pseudo-randomization of integers (e.g., to guard against bad hash functions).
|
static int |
checkedDownCast(long value)
Casts the given value to a 32 bit integer, if it can be safely done.
|
static boolean |
isPowerOf2(long value)
Checks whether the given value is a power of two.
|
static int |
jenkinsHash(int code)
This function hashes an integer value.
|
static int |
log2floor(int value)
Computes the logarithm of the given value to the base of 2, rounded down.
|
static int |
log2strict(int value)
Computes the logarithm of the given value to the base of 2.
|
static int |
longToIntWithBitMixing(long in)
Pseudo-randomly maps a long (64-bit) to an integer (32-bit) using some bit-mixing for better distribution.
|
static int |
murmurHash(int code)
This function hashes an integer value.
|
static int |
roundDownToPowerOf2(int value)
Decrements the given number down to the closest power of two.
|
static int |
roundUpToPowerOfTwo(int x)
Round the given number to the next power of two
|
public static int log2floor(int value) throws ArithmeticException
log2floor(16) = 4
, and
log2floor(10) = 3
.value
- The value to compute the logarithm for.ArithmeticException
- Thrown, if the given value is zero.public static int log2strict(int value) throws ArithmeticException, IllegalArgumentException
value
- The value to compute the logarithm for.ArithmeticException
- Thrown, if the given value is zero.IllegalArgumentException
- Thrown, if the given value is not a power of two.public static int roundDownToPowerOf2(int value)
value
- The value to round down.public static int checkedDownCast(long value)
This method is a protection in places where one expects to be able to safely case, but where unexpected situations could make the cast unsafe and would cause hidden problems that are hard to track down.
value
- The value to be cast to an integer.Math.toIntExact(long)
public static boolean isPowerOf2(long value)
value
- The value to check.public static int jenkinsHash(int code)
code
- The integer to be hashed.public static int murmurHash(int code)
code
- The integer to be hashed.public static int roundUpToPowerOfTwo(int x)
x
- number to roundpublic static int longToIntWithBitMixing(long in)
in
- the long (64-bit)input.public static int bitMix(int in)
in
- the input valueCopyright © 2014–2018 The Apache Software Foundation. All rights reserved.