Class PowerOf2Util


  • public final class PowerOf2Util
    extends java.lang.Object
    Collection of bit-tricks for power of 2 cases.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int align​(int value, int pow2alignment)
      Fast alignment operation with power of 2 alignment and value >=0 and value <Integer.MAX_VALUE.
      In order to be fast is up to the caller to check arguments correctness.
      static boolean isAligned​(long value, int pow2alignment)
      Test if a value is pow2alignment-aligned.
      static boolean isPowOf2​(int value)
      Is a value a positive power of two.
      • Methods inherited from class java.lang.Object

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

      • align

        public static int align​(int value,
                                int pow2alignment)
        Fast alignment operation with power of 2 alignment and value >=0 and value <Integer.MAX_VALUE.
        In order to be fast is up to the caller to check arguments correctness. Original algorithm is on https://en.wikipedia.org/wiki/Data_structure_alignment.
      • isPowOf2

        public static boolean isPowOf2​(int value)
        Is a value a positive power of two.
        Parameters:
        value - to be checked.
        Returns:
        true if the number is a positive power of two otherwise false.
      • isAligned

        public static boolean isAligned​(long value,
                                        int pow2alignment)
        Test if a value is pow2alignment-aligned.
        Parameters:
        value - to be tested.
        pow2alignment - boundary the address is tested against.
        Returns:
        true if the address is on the aligned boundary otherwise false.
        Throws:
        java.lang.IllegalArgumentException - if the alignment is not a power of 2