Class Pow2


  • public final class Pow2
    extends java.lang.Object
    Power of 2 utility functions.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_POW2  
    • Constructor Summary

      Constructors 
      Constructor Description
      Pow2()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long align​(long value, int alignment)
      Align a value to the next multiple up of alignment.
      static boolean isPowerOfTwo​(int value)  
      static int roundToPowerOfTwo​(int value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Pow2

        public Pow2()
    • Method Detail

      • roundToPowerOfTwo

        public static int roundToPowerOfTwo​(int value)
        Parameters:
        value - from which next positive power of two will be found.
        Returns:
        the next positive power of 2, this value if it is a power of 2. Negative values are mapped to 1.
        Throws:
        java.lang.IllegalArgumentException - is value is more than MAX_POW2 or less than 0
      • isPowerOfTwo

        public static boolean isPowerOfTwo​(int value)
        Parameters:
        value - to be tested to see if it is a power of two.
        Returns:
        true if the value is a power of 2 otherwise false.
      • align

        public static long align​(long value,
                                 int alignment)
        Align a value to the next multiple up of alignment. If the value equals an alignment multiple then it is returned unchanged.
        Parameters:
        value - to be aligned up.
        alignment - to be used, must be a power of 2.
        Returns:
        the value aligned to the next boundary.