Java Number Round roundUpToNearestPowerOfTwoIfGreaterThanZero(final int value)

Here you can find the source of roundUpToNearestPowerOfTwoIfGreaterThanZero(final int value)

Description

round Up To Nearest Power Of Two If Greater Than Zero

License

Open Source License

Declaration

@SuppressWarnings({ "NumericCastThatLosesPrecision", "MagicNumber" })
    public static int roundUpToNearestPowerOfTwoIfGreaterThanZero(final int value) 

Method Source Code

//package com.java2s;
// The MIT License (MIT)

public class Main {
    @SuppressWarnings({ "NumericCastThatLosesPrecision", "MagicNumber" })
    public static int roundUpToNearestPowerOfTwoIfGreaterThanZero(final int value) {
        // Inspired by https://graphics.stanford.edu/~seander/bithacks.html
        long v = value;
        v--;/*w  w w.ja va 2 s.c  o m*/
        v |= v >> 1;
        v |= v >> 2;
        v |= v >> 4;
        v |= v >> 8;
        v |= v >> 16;
        v++;
        return (int) v;
    }
}

Related

  1. roundUpTo8(final long number)
  2. roundUpTo8(long val)
  3. roundUpToMultiple(long val, int factor)
  4. roundUpToNearest(double number, double nearest)
  5. roundUpToNearestEightBytes(long result)
  6. roundUpToPowerOf2(int number)
  7. roundUpToPowerOf2(long val)
  8. roundUpToPowerOf2Factor(long val, long factor)
  9. roundUpToPowerOfTwo(int i)