All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.util.QuickMath Maven / Gradle / Ivy

There is a newer version: 5.0-BETA-1
Show newest version
package com.hazelcast.util;

/**
 * The class {@code 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.
 *
 *
 */
public final class QuickMath {

    private QuickMath() { }

    /**
     * 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.
     *
     * @param x
     * @return true if x is power of two
     */
    public static boolean isPowerOfTwo(int x) {
        return (x & (x - 1)) == 0;
    }

    /**
     * Computes the remainder of the division of a by b.
     * a has to be non-negative integer and b has to be power of two
     * otherwise the result is undefined.
     *
     * @param a
     * @param b
     * @return remainder of the division of a by b.
     */
    public static int mod(int a, int b) {
        return a & (b - 1);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy