Java Ceil ceilPowerOf2Bits(final long x)

Here you can find the source of ceilPowerOf2Bits(final long x)

Description

Get the smallest power of 2 > x.

License

Open Source License

Parameter

Parameter Description
x number being checked.

Return

power of 2 > x.

Declaration

public static int ceilPowerOf2Bits(final long x) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  ww . j a  va  2 s .c  o m
     * Get the smallest power of 2 > x. Undefined for negative numbers or numbers
     * so large that there is no positive power of 2 available.
     *
     * @param x number being checked.
     * @return power of 2 > x.
     */
    public static int ceilPowerOf2Bits(final long x) {
        if (x >= (1L << 62) || x < 0) {
            throw new IllegalArgumentException("Number out of range:" + x);
        }
        long i = 1L;
        int n = 0;
        while (i <= x) {
            assert i > 0;
            assert n >= 0;
            n++;
            i = i << 1;
        }
        assert 1L << n == i;
        return n;
    }
}

Related

  1. ceilPositive(float x)
  2. ceilPoT(int arg)
  3. ceilPow2(int v)
  4. ceilPowerOf2(final long x)
  5. ceilPowerOf2(int x)
  6. ceilPrime(long p)
  7. ceilSec(long milli)
  8. ceilToBase(int number, int base)
  9. ceilToPowerOfTwo(float value)