Java Power of 2 nextPowerOfTwo(int x)

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

Description

Calculates an integer that is a power of two and is equal or greater than a given integer

License

Open Source License

Parameter

Parameter Description
x the minimum value to return

Return

an integer 2^n which is equal or greater than x

Declaration

public static int nextPowerOfTwo(int x) 

Method Source Code

//package com.java2s;
/*// w w w .jav  a  2 s . co m
 *  MathUtil.java
 *  Eisenkraut
 *
 *  Copyright (c) 2004-2015 Hanns Holger Rutz. All rights reserved.
 *
 *  This software is published under the GNU General Public License v3+
 *
 *
 *   For further information, please contact Hanns Holger Rutz at
 *   contact@sciss.de
 */

public class Main {
    /**
     *  Calculates an integer that is a power
     *   of two and is equal or greater than a given integer
     *
     *   @param   x   the minimum value to return
     *   @return      an integer 2^n which is equal or greater than x
     */
    public static int nextPowerOfTwo(int x) {
        int y;
        for (y = 1; y < x; y <<= 1)
            ;
        return y;
    }
}

Related

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