Class BitUtil


  • public final class BitUtil
    extends java.lang.Object
    A variety of high efficiency bit twiddling routines and encoders for primitives.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long MAGIC0  
      private static long MAGIC1  
      private static long MAGIC2  
      private static long MAGIC3  
      private static long MAGIC4  
      private static long MAGIC5  
      private static long MAGIC6  
      static java.nio.ByteOrder NATIVE_BYTE_ORDER
      Native byte order.
      private static long SHIFT0  
      private static long SHIFT1  
      private static long SHIFT2  
      private static long SHIFT3  
      private static long SHIFT4  
      static java.lang.invoke.VarHandle VH_BE_DOUBLE
      Deprecated.
      Better use little endian unless it is needed for backwards compatibility.
      static java.lang.invoke.VarHandle VH_BE_FLOAT
      Deprecated.
      Better use little endian unless it is needed for backwards compatibility.
      static java.lang.invoke.VarHandle VH_BE_INT
      Deprecated.
      Better use little endian unless it is needed for backwards compatibility.
      static java.lang.invoke.VarHandle VH_BE_LONG
      Deprecated.
      Better use little endian unless it is needed for backwards compatibility.
      static java.lang.invoke.VarHandle VH_BE_SHORT
      Deprecated.
      Better use little endian unless it is needed for backwards compatibility.
      static java.lang.invoke.VarHandle VH_LE_DOUBLE
      A VarHandle to read/write little endian double from a byte array.
      static java.lang.invoke.VarHandle VH_LE_FLOAT
      A VarHandle to read/write little endian float from a byte array.
      static java.lang.invoke.VarHandle VH_LE_INT
      A VarHandle to read/write little endian int from a byte array.
      static java.lang.invoke.VarHandle VH_LE_LONG
      A VarHandle to read/write little endian long from a byte array.
      static java.lang.invoke.VarHandle VH_LE_SHORT
      A VarHandle to read/write little endian short from/to a byte array.
      static java.lang.invoke.VarHandle VH_NATIVE_DOUBLE
      A VarHandle to read/write native endian double from a byte array.
      static java.lang.invoke.VarHandle VH_NATIVE_FLOAT
      A VarHandle to read/write native endian float from a byte array.
      static java.lang.invoke.VarHandle VH_NATIVE_INT
      A VarHandle to read/write native endian int from a byte array.
      static java.lang.invoke.VarHandle VH_NATIVE_LONG
      A VarHandle to read/write native endian long from a byte array.
      static java.lang.invoke.VarHandle VH_NATIVE_SHORT
      A VarHandle to read/write native endian short from/to a byte array.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private BitUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long deinterleave​(long b)
      Extract just the even-bits value as a long from the bit-interleaved value
      static long flipFlop​(long b)
      flip flops odd with even bits
      private static java.nio.ByteOrder getNativeByteOrder()  
      static long interleave​(int even, int odd)
      Interleaves the first 32 bits of each long value
      static int nextHighestPowerOfTwo​(int v)
      returns the next highest power of two, or the current value if it's already a power of two or zero
      static long nextHighestPowerOfTwo​(long v)
      returns the next highest power of two, or the current value if it's already a power of two or zero
      static int zigZagDecode​(int i)
      Decode an int previously encoded with zigZagEncode(int).
      static long zigZagDecode​(long l)
      Decode a long previously encoded with zigZagEncode(long).
      static int zigZagEncode​(int i)
      Same as zigZagEncode(long) but on integers.
      static long zigZagEncode​(long l)
      Zig-zag encode the provided long.
      • Methods inherited from class java.lang.Object

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

      • NATIVE_BYTE_ORDER

        public static final java.nio.ByteOrder NATIVE_BYTE_ORDER
        Native byte order.

        Warning: This constant is ByteOrder.nativeOrder() only in production environments, during testing we randomize it. If you need to communicate with native APIs (e.g., Java's Panama API), use ByteOrder.nativeOrder().

      • VH_LE_SHORT

        public static final java.lang.invoke.VarHandle VH_LE_SHORT
        A VarHandle to read/write little endian short from/to a byte array. Shape: short vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, short val)
      • VH_LE_INT

        public static final java.lang.invoke.VarHandle VH_LE_INT
        A VarHandle to read/write little endian int from a byte array. Shape: int vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, int val)
      • VH_LE_LONG

        public static final java.lang.invoke.VarHandle VH_LE_LONG
        A VarHandle to read/write little endian long from a byte array. Shape: long vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, long val)
      • VH_LE_FLOAT

        public static final java.lang.invoke.VarHandle VH_LE_FLOAT
        A VarHandle to read/write little endian float from a byte array. Shape: float vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, float val)
      • VH_LE_DOUBLE

        public static final java.lang.invoke.VarHandle VH_LE_DOUBLE
        A VarHandle to read/write little endian double from a byte array. Shape: double vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, double val)
      • VH_NATIVE_SHORT

        public static final java.lang.invoke.VarHandle VH_NATIVE_SHORT
        A VarHandle to read/write native endian short from/to a byte array. Shape: short vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, short val)

        Warning: This handle uses default order only in production environments, during testing we randomize it. If you need to communicate with native APIs (e.g., Java's Panama API), use ByteOrder.nativeOrder().

      • VH_NATIVE_INT

        public static final java.lang.invoke.VarHandle VH_NATIVE_INT
        A VarHandle to read/write native endian int from a byte array. Shape: int vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, int val)

        Warning: This handle uses default order only in production environments, during testing we randomize it. If you need to communicate with native APIs (e.g., Java's Panama API), use ByteOrder.nativeOrder().

      • VH_NATIVE_LONG

        public static final java.lang.invoke.VarHandle VH_NATIVE_LONG
        A VarHandle to read/write native endian long from a byte array. Shape: long vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, long val)

        Warning: This handle uses default order only in production environments, during testing we randomize it. If you need to communicate with native APIs (e.g., Java's Panama API), use ByteOrder.nativeOrder().

      • VH_NATIVE_FLOAT

        public static final java.lang.invoke.VarHandle VH_NATIVE_FLOAT
        A VarHandle to read/write native endian float from a byte array. Shape: float vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, float val)

        Warning: This handle uses default order only in production environments, during testing we randomize it. If you need to communicate with native APIs (e.g., Java's Panama API), use ByteOrder.nativeOrder().

      • VH_NATIVE_DOUBLE

        public static final java.lang.invoke.VarHandle VH_NATIVE_DOUBLE
        A VarHandle to read/write native endian double from a byte array. Shape: double vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, double val)

        Warning: This handle uses default order only in production environments, during testing we randomize it. If you need to communicate with native APIs (e.g., Java's Panama API), use ByteOrder.nativeOrder().

      • VH_BE_SHORT

        @Deprecated
        public static final java.lang.invoke.VarHandle VH_BE_SHORT
        Deprecated.
        Better use little endian unless it is needed for backwards compatibility.
        A VarHandle to read/write big endian short from a byte array. Shape: short vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, short val)
      • VH_BE_INT

        @Deprecated
        public static final java.lang.invoke.VarHandle VH_BE_INT
        Deprecated.
        Better use little endian unless it is needed for backwards compatibility.
        A VarHandle to read/write big endian int from a byte array. Shape: int vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, int val)
      • VH_BE_LONG

        @Deprecated
        public static final java.lang.invoke.VarHandle VH_BE_LONG
        Deprecated.
        Better use little endian unless it is needed for backwards compatibility.
        A VarHandle to read/write big endian long from a byte array. Shape: long vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, long val)
      • VH_BE_FLOAT

        @Deprecated
        public static final java.lang.invoke.VarHandle VH_BE_FLOAT
        Deprecated.
        Better use little endian unless it is needed for backwards compatibility.
        A VarHandle to read/write big endian float from a byte array. Shape: float vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, float val)
      • VH_BE_DOUBLE

        @Deprecated
        public static final java.lang.invoke.VarHandle VH_BE_DOUBLE
        Deprecated.
        Better use little endian unless it is needed for backwards compatibility.
        A VarHandle to read/write big endian double from a byte array. Shape: double vh.get(byte[] arr, int ofs) and void vh.set(byte[] arr, int ofs, double val)
    • Constructor Detail

      • BitUtil

        private BitUtil()
    • Method Detail

      • getNativeByteOrder

        private static java.nio.ByteOrder getNativeByteOrder()
      • nextHighestPowerOfTwo

        public static int nextHighestPowerOfTwo​(int v)
        returns the next highest power of two, or the current value if it's already a power of two or zero
      • nextHighestPowerOfTwo

        public static long nextHighestPowerOfTwo​(long v)
        returns the next highest power of two, or the current value if it's already a power of two or zero
      • interleave

        public static long interleave​(int even,
                                      int odd)
        Interleaves the first 32 bits of each long value

        Adapted from: http://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN

      • deinterleave

        public static long deinterleave​(long b)
        Extract just the even-bits value as a long from the bit-interleaved value
      • flipFlop

        public static long flipFlop​(long b)
        flip flops odd with even bits
      • zigZagEncode

        public static int zigZagEncode​(int i)
        Same as zigZagEncode(long) but on integers.
      • zigZagEncode

        public static long zigZagEncode​(long l)
        Zig-zag encode the provided long. Assuming the input is a signed long whose absolute value can be stored on n bits, the returned value will be an unsigned long that can be stored on n+1 bits.
      • zigZagDecode

        public static int zigZagDecode​(int i)
        Decode an int previously encoded with zigZagEncode(int).
      • zigZagDecode

        public static long zigZagDecode​(long l)
        Decode a long previously encoded with zigZagEncode(long).