Java Power of 2 nextPowerOf2(int x)

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

Description

Get the value that is equal or higher than this value, and that is a power of two.

License

Apache License

Parameter

Parameter Description
x the original value

Return

the next power of two value

Declaration

public static int nextPowerOf2(int x) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from  w  w w  .java2s  .c  o m
     * Get the value that is equal or higher than this value, and that is a
     * power of two.
     *
     * @param x the original value
     * @return the next power of two value
     */
    public static int nextPowerOf2(int x) {
        long i = 1;
        while (i < x && i < (Integer.MAX_VALUE / 2)) {
            i += i;
        }
        return (int) i;
    }
}

Related

  1. nextPowerOf2(int n)
  2. nextPowerOf2(int n)
  3. nextPowerOf2(int n)
  4. nextPowerOf2(int v)
  5. nextPowerOf2(int x)
  6. nextPowerOfTwo(final int targetSize)
  7. nextPowerOfTwo(final long nValue)
  8. nextPowerOfTwo(int i)
  9. nextPowerOfTwo(int i)