Java Number Round roundUpToTheNextHighestPowerOf2(int v)

Here you can find the source of roundUpToTheNextHighestPowerOf2(int v)

Description

Compute the power of 2 which produces an number containing some particular value.

License

Open Source License

Parameter

Parameter Description
v the value for which we need to find the power of 2 such that number is greater than or equal to v.

Return

a number such that 2 to the power of that number contains v

Declaration

public static int roundUpToTheNextHighestPowerOf2(int v) 

Method Source Code

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

public class Main {
    /**/*  w  ww.ja v a 2s  .  c o  m*/
     * Compute the power of 2 which produces an number containing some particular 
     * value.
     * 
     * @param v the value for which we need to find the power of 2 such that
     * number is greater than or equal to v.
     * 
     * @return a number such that 2 to the power of that number contains v
     */
    public static int roundUpToTheNextHighestPowerOf2(int v) {
        // from: http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
        v--;
        v |= v >> 1;
        v |= v >> 2;
        v |= v >> 4;
        v |= v >> 8;
        v |= v >> 16;
        v++;
        return v;
    }
}

Related

  1. roundUpToPowerOf2Factor(long val, long factor)
  2. roundUpToPowerOfTwo(int i)
  3. roundUpToPowerOfTwo(int p_151236_0_)
  4. roundUpToPowerOfTwo(int v)
  5. roundUpToPowerOfTwo(int x)
  6. roundUpToTwo(Double number)
  7. roundValue(Double value)
  8. roundValue(final double value)
  9. roundValues(Double value1, Double value2)