get Closest Power Of Two - Android java.lang

Android examples for java.lang:Math

Description

get Closest Power Of Two

Demo Code


//package com.java2s;

public class Main {
    public static int getClosestPowerOfTwo(int value) {
        value--;//  ww  w .  j av  a2s  . c  o  m
        value |= value >> 1;
        value |= value >> 2;
        value |= value >> 4;
        value |= value >> 8;
        value |= value >> 16;

        if (value == Integer.MAX_VALUE)
            return Integer.MAX_VALUE;
        value++;

        return value;
    }
}

Related Tutorials