Java Ceil ceilingPowerOfTwo(final int a)

Here you can find the source of ceilingPowerOfTwo(final int a)

Description

ceiling Power Of Two

License

Open Source License

Parameter

Parameter Description
a A value in [0,2^30].

Return

The lowest power of two >= a.

Declaration

public static int ceilingPowerOfTwo(final int a) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from  ww w . j a v a2 s .c  o m*/
     * @param a
     *            A value in [0,2^30].
     * @return The lowest power of two >= a.
     */
    public static int ceilingPowerOfTwo(final int a) {
        checkIsInRange(0, 1 << 30, a);
        return a >= 2 ? Integer.highestOneBit(a - 1 << 1) : 1;
    }

    /**
     * @param a
     *            A value in [0,2^62].
     * @return The lowest power of two >= a.
     */
    public static long ceilingPowerOfTwo(final long a) {
        checkIsInRange(0L, 1L << 62, a);
        // Faster than copying int method
        // (less computations on long).
        return 1L << 64 - Long.numberOfLeadingZeros(a - 1);
    }

    /**
     * @return True if does not throw.
     * @throws IllegalArgumentException
     *             if the specified value is not in the specified range (inclusive).
     */
    public static boolean checkIsInRange(final int min, final int max, final int a) {
        if (!isInRange(min, max, a)) {
            throw new IllegalArgumentException(a + " not in [" + min + "," + max + "]");
        }
        return true;
    }

    /**
     * @return True if does not throw.
     * @throws IllegalArgumentException
     *             if the specified value is not in the specified range (inclusive).
     */
    public static boolean checkIsInRange(final long min, final long max, final long a) {
        if (!isInRange(min, max, a)) {
            throw new IllegalArgumentException(a + " not in [" + min + "," + max + "]");
        }
        return true;
    }

    /**
     * @return True if does not throw.
     * @throws IllegalArgumentException
     *             if the specified value is not in the specified range (inclusive) or any parameter is NaN.
     */
    public static boolean checkIsInRange(final float min, final float max, final float a) {
        if (!isInRange(min, max, a)) {
            throw new IllegalArgumentException(a + " not in [" + min + "," + max + "]");
        }
        return true;
    }

    /**
     * @return True if does not throw.
     * @throws IllegalArgumentException
     *             if the specified value is not in the specified range (inclusive) or any parameter is NaN.
     */
    public static boolean checkIsInRange(final double min, final double max, final double a) {
        if (!isInRange(min, max, a)) {
            throw new IllegalArgumentException(a + " not in [" + min + "," + max + "]");
        }
        return true;
    }

    /**
     * @return True if the specified value is in the specified range (inclusive), false otherwise.
     */
    public static boolean isInRange(final int min, final int max, final int a) {
        return min <= a && a <= max;
    }

    /**
     * @return True if the specified value is in the specified range (inclusive), false otherwise.
     */
    public static boolean isInRange(final long min, final long max, final long a) {
        return min <= a && a <= max;
    }

    /**
     * Returns false if any value is NaN.
     *
     * @return True if the specified value is in the specified range (inclusive), false otherwise.
     */
    public static boolean isInRange(final float min, final float max, final float a) {
        return min <= a && a <= max;
    }

    /**
     * Returns false if any value is NaN.
     *
     * @return True if the specified value is in the specified range (inclusive), false otherwise.
     */
    public static boolean isInRange(final double min, final double max, final double a) {
        return min <= a && a <= max;
    }
}

Related

  1. ceilingHalf(int num)
  2. ceilingNextPowerOfTwo(final int x)
  3. ceilingPow2(int n)
  4. ceilingPowerOf2(final int n)
  5. ceilingPowerOf2(int n)
  6. ceilingPowerOfTwo(int value)
  7. ceilInt(Double d, double intv)
  8. ceilInt(final double x)
  9. ceilLogBaseTwo(final int i)