Java Power of 2 nextPowerOf2(final int n)

Here you can find the source of nextPowerOf2(final int n)

Description

Returns the next power of 2 superior to n.

License

Open Source License

Parameter

Parameter Description
n The value to find the next power of 2 above

Return

The next power of 2

Declaration

public static int nextPowerOf2(final int n) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  ww.  j  a va 2  s.c om*/
     * Returns the next power of 2 superior to n.
     *
     * @param n
     *            The value to find the next power of 2 above
     * @return The next power of 2
     */
    public static int nextPowerOf2(final int n) {
        return (int) Math.pow(2, 32 - Integer.numberOfLeadingZeros(n - 1));
    }
}

Related

  1. nextPower(int v, int power)
  2. nextPower2(int n)
  3. nextPowerOf2(int n)
  4. nextPowerOf2(int n)
  5. nextPowerOf2(int n)
  6. nextPowerOf2(int n)