Java Power of 2 nextPowerOf2(int n)

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

Description

Returns the next power of 2 that is greater or equal to the specified value.

License

Open Source License

Parameter

Parameter Description
n The value

Return

The next power of power of 2

Declaration

public static int nextPowerOf2(int n) 

Method Source Code

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

public class Main {
    /**/*  ww w.  j a  va 2  s.  c om*/
     * Returns the next power of 2 that is greater or equal to the specified value.
     *
     * @param n The value
     * @return The next power of power of 2
     */
    public static int nextPowerOf2(int n) {
        assert n > 0;

        n--;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return n + 1;
    }
}

Related

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