Package org.agrona

Class BitUtil

java.lang.Object
org.agrona.BitUtil

public final class BitUtil extends Object
Miscellaneous useful functions for dealing with low level bits and bytes.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Length of the data blocks used by the CPU cache sub-system in bytes.
    private static final byte[]
     
    private static final byte[]
     
    private static final int
     
    static final int
    Size of a boolean in bytes.
    static final int
    Size of a byte in bytes.
    static final int
    Size of a char in bytes.
    static final int
    Size of a double in bytes.
    static final int
    Size of a float in bytes.
    static final int
    Size of an int in bytes.
    static final int
    Size of a long in bytes.
    static final int
    Size of a short in bytes.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    align(int value, int alignment)
    Align a value to the next multiple up of alignment.
    static long
    align(long value, long alignment)
    Align a value to the next multiple up of alignment.
    static int
    Calculate the shift value to scale a number based on how refs are compressed or not.
    static int
    Fast method of finding the next power of 2 greater than or equal to the supplied value.
    static long
    Fast method of finding the next power of 2 greater than or equal to the supplied value.
    static byte[]
    fromHex(String string)
    Generate a byte array from a string that is the hex representation of the given byte array.
    static byte[]
    fromHexByteArray(byte[] buffer)
    Generate a byte array from the hex representation of the given byte array.
    static int
    Generate a randomised integer over [Integer.MIN_VALUE, Integer.MAX_VALUE].
    static boolean
    isAligned(long address, int alignment)
    Is an address aligned on a boundary.
    static boolean
    isEven(int value)
    Is a int value even.
    static boolean
    isEven(long value)
    Is a long value even.
    static boolean
    isPowerOfTwo(int value)
    Is a value a positive power of 2.
    static boolean
    isPowerOfTwo(long value)
    Is a value a positive power of 2.
    static int
    next(int current, int max)
    Cycles indices of an array one at a time in a forward fashion.
    static int
    previous(int current, int max)
    Cycles indices of an array one at a time in a backwards fashion.
    static String
    toHex(byte[] buffer)
    Generate a string that is the hex representation of a given byte array.
    static String
    toHex(byte[] buffer, int offset, int length)
    Generate a string that is the hex representation of a given byte array.
    static byte[]
    toHexByteArray(byte[] buffer)
    Generate a byte array that is a hex representation of a given byte array.
    static byte[]
    toHexByteArray(byte[] buffer, int offset, int length)
    Generate a byte array that is a hex representation of a given byte array.
    static byte[]
    toHexByteArray(CharSequence charSequence, int offset, int length)
    Generate a byte array that is a hex representation of a given byte array.

    Methods inherited from class java.lang.Object

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

    • SIZE_OF_BYTE

      public static final int SIZE_OF_BYTE
      Size of a byte in bytes.
      See Also:
    • SIZE_OF_BOOLEAN

      public static final int SIZE_OF_BOOLEAN
      Size of a boolean in bytes.
      See Also:
    • SIZE_OF_CHAR

      public static final int SIZE_OF_CHAR
      Size of a char in bytes.
      See Also:
    • SIZE_OF_SHORT

      public static final int SIZE_OF_SHORT
      Size of a short in bytes.
      See Also:
    • SIZE_OF_INT

      public static final int SIZE_OF_INT
      Size of an int in bytes.
      See Also:
    • SIZE_OF_FLOAT

      public static final int SIZE_OF_FLOAT
      Size of a float in bytes.
      See Also:
    • SIZE_OF_LONG

      public static final int SIZE_OF_LONG
      Size of a long in bytes.
      See Also:
    • SIZE_OF_DOUBLE

      public static final int SIZE_OF_DOUBLE
      Size of a double in bytes.
      See Also:
    • CACHE_LINE_LENGTH

      public static final int CACHE_LINE_LENGTH
      Length of the data blocks used by the CPU cache sub-system in bytes.
      See Also:
    • HEX_DIGIT_TABLE

      private static final byte[] HEX_DIGIT_TABLE
    • FROM_HEX_DIGIT_TABLE

      private static final byte[] FROM_HEX_DIGIT_TABLE
    • LAST_DIGIT_MASK

      private static final int LAST_DIGIT_MASK
      See Also:
  • Constructor Details

    • BitUtil

      private BitUtil()
  • Method Details

    • findNextPositivePowerOfTwo

      public static int findNextPositivePowerOfTwo(int value)
      Fast method of finding the next power of 2 greater than or equal to the supplied value.

      If the value is <= 0 then 1 will be returned.

      This method is not suitable for Integer.MIN_VALUE or numbers greater than 2^30. When provided then Integer.MIN_VALUE will be returned.

      Parameters:
      value - from which to search for next power of 2.
      Returns:
      The next power of 2 or the value itself if it is a power of 2.
    • findNextPositivePowerOfTwo

      public static long findNextPositivePowerOfTwo(long value)
      Fast method of finding the next power of 2 greater than or equal to the supplied value.

      If the value is <= 0 then 1 will be returned.

      This method is not suitable for Long.MIN_VALUE or numbers greater than 2^62. When provided then Long.MIN_VALUE will be returned.

      Parameters:
      value - from which to search for next power of 2.
      Returns:
      The next power of 2 or the value itself if it is a power of 2.
    • align

      public static int align(int 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.

      This method executes without branching. This code is designed to be use in the fast path and should not be used with negative numbers. Negative numbers will result in undefined behaviour.

      Parameters:
      value - to be aligned up.
      alignment - to be used.
      Returns:
      the value aligned to the next boundary.
    • align

      public static long align(long value, long alignment)
      Align a value to the next multiple up of alignment. If the value equals an alignment multiple then it is returned unchanged.

      This method executes without branching. This code is designed to be use in the fast path and should not be used with negative numbers. Negative numbers will result in undefined behaviour.

      Parameters:
      value - to be aligned up.
      alignment - to be used.
      Returns:
      the value aligned to the next boundary.
    • fromHexByteArray

      public static byte[] fromHexByteArray(byte[] buffer)
      Generate a byte array from the hex representation of the given byte array.
      Parameters:
      buffer - to convert from a hex representation (in Big Endian).
      Returns:
      new byte array that is decimal representation of the passed array.
    • toHexByteArray

      public static byte[] toHexByteArray(byte[] buffer)
      Generate a byte array that is a hex representation of a given byte array.
      Parameters:
      buffer - to convert to a hex representation.
      Returns:
      new byte array that is hex representation (in Big Endian) of the passed array.
    • toHexByteArray

      public static byte[] toHexByteArray(byte[] buffer, int offset, int length)
      Generate a byte array that is a hex representation of a given byte array.
      Parameters:
      buffer - to convert to a hex representation.
      offset - the offset into the buffer.
      length - the number of bytes to convert.
      Returns:
      new byte array that is hex representation (in Big Endian) of the passed array.
    • toHexByteArray

      public static byte[] toHexByteArray(CharSequence charSequence, int offset, int length)
      Generate a byte array that is a hex representation of a given byte array.
      Parameters:
      charSequence - to convert to a hex representation.
      offset - the offset into the buffer.
      length - the number of bytes to convert.
      Returns:
      new byte array that is hex representation (in Big Endian) of the passed array.
    • fromHex

      public static byte[] fromHex(String string)
      Generate a byte array from a string that is the hex representation of the given byte array.
      Parameters:
      string - to convert from a hex representation (in Big Endian).
      Returns:
      new byte array holding the decimal representation of the passed array.
    • toHex

      public static String toHex(byte[] buffer, int offset, int length)
      Generate a string that is the hex representation of a given byte array.
      Parameters:
      buffer - to convert to a hex representation.
      offset - the offset into the buffer.
      length - the number of bytes to convert.
      Returns:
      new String holding the hex representation (in Big Endian) of the passed array.
    • toHex

      public static String toHex(byte[] buffer)
      Generate a string that is the hex representation of a given byte array.
      Parameters:
      buffer - to convert to a hex representation.
      Returns:
      new String holding the hex representation (in Big Endian) of the passed array.
    • isEven

      public static boolean isEven(int value)
      Is a int value even.
      Parameters:
      value - to check.
      Returns:
      true if the number is even otherwise false.
    • isEven

      public static boolean isEven(long value)
      Is a long value even.
      Parameters:
      value - to check.
      Returns:
      true if the number is even otherwise false.
    • isPowerOfTwo

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

      public static boolean isPowerOfTwo(long value)
      Is a value a positive power of 2.
      Parameters:
      value - to be checked.
      Returns:
      true if the number is a positive power of 2, otherwise false.
    • next

      public static int next(int current, int max)
      Cycles indices of an array one at a time in a forward fashion.
      Parameters:
      current - value to be incremented.
      max - value for the cycle.
      Returns:
      the next value, or zero if max is reached.
    • previous

      public static int previous(int current, int max)
      Cycles indices of an array one at a time in a backwards fashion.
      Parameters:
      current - value to be decremented.
      max - value of the cycle.
      Returns:
      the next value, or max - 1 if current is zero.
    • calculateShiftForScale

      public static int calculateShiftForScale(int scale)
      Calculate the shift value to scale a number based on how refs are compressed or not.
      Parameters:
      scale - of the number reported by Unsafe.
      Returns:
      how many times the number needs to be shifted to the left.
    • generateRandomisedId

      public static int generateRandomisedId()
      Generate a randomised integer over [Integer.MIN_VALUE, Integer.MAX_VALUE].
      Returns:
      randomised integer suitable as an ID.
    • isAligned

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