com.hazelcast.util

Class QuickMath

• public final class QuickMath
extends Object
The class QuickMath contains methods to perform optimized mathematical operations. Methods are allowed to put additional constraints on the range of input values if required for efficiency. Methods are not required to perform validation of input arguments, but they have to indicate the constraints in theirs contract.
• Method Summary

Methods
Modifier and Type Method and Description
static String bytesToHex(byte[] in)
static int compareIntegers(int i1, int i2)
Compares two integers
static int compareLongs(long l1, long l2)
Compares two longs
static int divideByAndCeilToInt(double d, int k)
Divide d by k and return the smallest integer greater than or equal to the result.
static long divideByAndCeilToLong(double d, int k)
Divide d by k and return the smallest integer greater than or equal to the result.
static int divideByAndRoundToInt(double d, int k)
Divide d by k and return the int value closest to the result.
static long divideByAndRoundToLong(double d, int k)
Divide d by k and return the long value closest to the result.
static boolean isPowerOfTwo(long x)
Return true if input argument is power of two.
static int log2(int value)
Return the log 2 result for this int.
static int log2(long value)
Return the log 2 result for this long.
static int modPowerOfTwo(int a, int b)
Computes the remainder of the division of a by b.
static long modPowerOfTwo(long a, int b)
Computes the remainder of the division of a by b.
static int nextPowerOfTwo(int value)
Fast method of finding the next power of 2 greater than or equal to the supplied value.
static long nextPowerOfTwo(long value)
Fast method of finding the next power of 2 greater than or equal to the supplied value.
static int normalize(int value, int factor)
Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.
static long normalize(long value, int factor)
Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.
• Method Detail

• isPowerOfTwo

public static boolean isPowerOfTwo(long x)
Return true if input argument is power of two. Input has to be a a positive integer.

Result is undefined for zero or negative integers.

Parameters:
x - test x to see if it is a power of two
Returns:
true if x is power of two
• modPowerOfTwo

public static int modPowerOfTwo(int a,
int b)
Computes the remainder of the division of a by b. a has to be a non-negative integer and b has to be a power of two, otherwise the result is undefined.
Parameters:
a - divide a by b. a must be a non-negative integer
b - divide a by b. b must be a power of two
Returns:
remainder of the division of a by b.
• modPowerOfTwo

public static long modPowerOfTwo(long a,
int b)
Computes the remainder of the division of a by b. a has to be a non-negative integer and b has to be a power of two, otherwise the result is undefined.
Parameters:
a - divide a by b. a must be a non-negative integer
b - divide a by b. b must be a power of two
Returns:
remainder of the division of a by b.
• nextPowerOfTwo

public static int nextPowerOfTwo(int value)
Fast method of finding the next power of 2 greater than or equal to the supplied value.

If the value is <= 0 then 1 will be returned.

This method is not suitable for Integer.MIN_VALUE or numbers greater than 2^30.

Parameters:
value - from which to search for next power of 2
Returns:
The next power of 2 or the value itself if it is a power of 2
• nextPowerOfTwo

public static long nextPowerOfTwo(long value)
Fast method of finding the next power of 2 greater than or equal to the supplied value.

If the value is <= 0 then 1 will be returned.

This method is not suitable for Long.MIN_VALUE or numbers greater than 2^62.

Parameters:
value - from which to search for next power of 2
Returns:
The next power of 2 or the value itself if it is a power of 2
• log2

public static int log2(int value)
Return the log 2 result for this int.
Parameters:
value - the int value
Returns:
the log 2 result for value
• log2

public static int log2(long value)
Return the log 2 result for this long.
Parameters:
value - the long value
Returns:
the log 2 result for value
• divideByAndCeilToInt

public static int divideByAndCeilToInt(double d,
int k)
Divide d by k and return the smallest integer greater than or equal to the result.
Parameters:
d - divide d by k
k - divide d by k
Returns:
the smallest integer greater than or equal to the result
• divideByAndCeilToLong

public static long divideByAndCeilToLong(double d,
int k)
Divide d by k and return the smallest integer greater than or equal to the result.
Parameters:
d - divide d by k
k - divide d by k
Returns:
the smallest integer greater than or equal to the result
• divideByAndRoundToInt

public static int divideByAndRoundToInt(double d,
int k)
Divide d by k and return the int value closest to the result.
Parameters:
d - divide d by k
k - divide d by k
Returns:
the int value closest to the result
• divideByAndRoundToLong

public static long divideByAndRoundToLong(double d,
int k)
Divide d by k and return the long value closest to the result.
Parameters:
d - divide d by k
k - divide d by k
Returns:
the long value closest to the result
• normalize

public static int normalize(int value,
int factor)
Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.
Parameters:
value - normalize this value by factor
factor - normalize this value by factor
Returns:
the result of value being normalized by factor
• normalize

public static long normalize(long value,
int factor)
Divide value by factor, take the smallest integer greater than or equal to the result, multiply that integer by factor, and return it.
Parameters:
value - normalize this value by factor
factor - normalize this value by factor
Returns:
the result of value being normalized by factor
• bytesToHex

public static String bytesToHex(byte[] in)
• compareIntegers

public static int compareIntegers(int i1,
int i2)
Compares two integers
Parameters:
i1 - First number to compare with second one
i2 - Second number to compare with first one
Returns:
+1 if i1 > i2, -1 if i2 > i1, 0 if i1 and i2 are equals
• compareLongs

public static int compareLongs(long l1,
long l2)
Compares two longs
Parameters:
l1 - First number to compare with second one
l2 - Second number to compare with first one
Returns:
+1 if l1 > l2, -1 if l2 > l1, 0 if l1 and l2 are equals