Class Bitfield.Util

  • Enclosing interface:

    public static class Bitfield.Util
    extends Object
    Bit operation utilities (static).
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • getBitMask

        public static int getBitMask​(int n)
        Returns the 32 bit mask of n-bits, i.e. n low order 1’s.

        Implementation handles n == 32.

        IndexOutOfBoundsException - if b is out of bounds, i.e. > 32
      • bitCount

        public static final int bitCount​(int n)
        Returns the number of set bits within given 32bit integer in O(1) using a HAKEM 169 Bit Count inspired implementation:

        We rely on the JVM spec Integer.SIZE == 32.

      • isPowerOf2

        public static final boolean isPowerOf2​(int n)
        Returns true if the given integer is a power of 2

        Source: bithacks:

      • nextPowerOf2

        public static final int nextPowerOf2​(int n)
        Returns the next higher power of 2 of 32-bit of given n

        Source: bithacks:

        We rely on the JVM spec Integer.SIZE == 32.

      • roundToPowerOf2

        public static final int roundToPowerOf2​(int n)
        If the given n is not isPowerOf2(int) return nextPowerOf2(int), otherwise return n unchanged.
         return isPowerOf2(n) ? n : nextPowerOf2(n);

        We rely on the JVM spec Integer.SIZE == 32.