Java Number Round roundToPowerOf2(int value)

Here you can find the source of roundToPowerOf2(int value)

Description

round To Power Of

License

LGPL

Declaration

public static int roundToPowerOf2(int value) 

Method Source Code

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

public class Main {
    public static int roundToPowerOf2(int value) {
        value--;//  w  w w  .j a va 2s  .  c o  m
        value |= value >> 1;
        value |= value >> 2;
        value |= value >> 4;
        value |= value >> 8;
        return value + 1;
    }

    public static long roundToPowerOf2(long value) {
        value--;
        value |= value >> 1;
        value |= value >> 2;
        value |= value >> 4;
        value |= value >> 8;
        return value + 1;
    }
}

Related

  1. roundToNextPowerOfTwo(int value)
  2. roundToNthDecimal(double number, int decimals)
  3. roundToNumDigits(double d, int n)
  4. roundToPowerOf(int i, int powerOf)
  5. roundToPowerOf10(double value, int powerOf10)
  6. roundToPrecision(double number, int precision)
  7. roundToPrecision(float value, int numDecimalPlaces)
  8. roundToQuarterMs(float f)
  9. roundToShort(final double d)