Java Power of 2 nextPowerOfTwoExact(int n)

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

Description

Finds the first power of two that is equal to or greater than n.

License

Apache License

Parameter

Parameter Description
n the number to find the next power of two for

Return

the next power of two after or equal to n, with a minimum of 0

Declaration

public static int nextPowerOfTwoExact(int n) 

Method Source Code

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

public class Main {
    /**//w  ww. ja  v  a2s .c  o  m
     * Finds the first power of two that is equal to or greater than n.
     * @param n the number to find the next power of two for
     * @return the next power of two after or equal to n, with a minimum of 0
     */
    public static int nextPowerOfTwoExact(int n) {
        int highest = Integer.highestOneBit(n);
        return (highest == Integer.lowestOneBit(n)) ? highest
                : highest << 1;
    }
}

Related

  1. nextPowerOfTwo(int x)
  2. nextPowerOfTwo(int x)
  3. nextPowerOfTwo(int x)
  4. nextPowerOfTwo(int x)
  5. nextPowerOfTwo(int x)
  6. nextPowerOfTwoValue(double value)
  7. nextPowerTwo(int initialNum)