Java Ceil ceilPow2(int v)

Here you can find the source of ceilPow2(int v)

Description

Gets nearest power of 2 larger or equal than v.

License

Open Source License

Parameter

Parameter Description
v Value.

Return

Nearest power of 2.

Declaration

public static int ceilPow2(int v) 

Method Source Code

//package com.java2s;
/* //from  w  w  w.j av  a  2  s  . c om
 Copyright (C) GridGain Systems. All Rights Reserved.
     
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
     
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

public class Main {
    /**
     * Gets nearest power of 2 larger or equal than v.
     *
     * @param v Value.
     * @return Nearest power of 2.
     */
    public static int ceilPow2(int v) {
        v--;

        v |= v >> 1;
        v |= v >> 2;
        v |= v >> 4;
        v |= v >> 8;
        v |= v >> 16;

        return ++v;
    }
}

Related

  1. ceilLogBaseTwo(final int i)
  2. ceilMaskPOT(int n)
  3. ceilMultiple(float source, float multiple)
  4. ceilPositive(float x)
  5. ceilPoT(int arg)
  6. ceilPowerOf2(final long x)
  7. ceilPowerOf2(int x)
  8. ceilPowerOf2Bits(final long x)
  9. ceilPrime(long p)