Class Bits

java.lang.Object
io.jenetics.internal.util.Bits

public final class Bits extends Object
Some bit utils. All operation assume little-endian byte order.
  Byte:       3        2        1        0
              |        |        |        |
  Array: |11110011|10011101|01000000|00101010|
          |                 |        |      |
  Bit:    23                15       7      0
 
Since:
1.0
Version:
5.2
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    complement(byte[] data)
    Make the two's complement of the given data array.
    static byte[]
    copy(byte[] data, int start, int end)
    Copies the specified range of the specified array into a new array.
    static int
    count(byte value)
    Returns the number of one-bits in the given byte value.
    static int
    count(byte[] data)
    Returns the number of one-bits in the given byte[] array.
    static int
    count(byte[] bits, int start, int end)
    Returns the number of one-bits in the given byte[] array.
    static byte[]
    flip(byte[] data, int index)
    Flip the bit at the given index.
    static byte[]
    Convert a string which was created with the toByteString(byte...) method back to an byte array.
    static boolean
    get(byte[] data, int index)
    Return the (boolean) value of the byte array at the given bit index.
    static boolean
    getAndSet(byte[] array, int index)
     
    static byte[]
    increment(byte[] data)
    Increment the given data array.
    static byte[]
    invert(byte[] data)
    Invert the given data array.
    static byte[]
    newArray(int length)
    Create a new byte[] array which can store at least the number of bits as defined by the given length parameter.
    static byte[]
    newArray(int length, double p)
    Create a new byte[] array which can store at least the number of bits as defined by the given length parameter.
    static byte[]
    reverse(byte[] array)
     
    static byte[]
    set(byte[] data, int index)
    Set the bit in the given byte array at the bit position (not the index within the byte array) to true.
    static byte[]
    set(byte[] data, int index, boolean value)
    Set the bit in the given byte array at the bit position (not the index within the byte array) to the specified value.
    static byte[]
    shiftLeft(byte[] data, int shift)
    Shifting all bits in the given data array the given shift to the left.
    static byte[]
    shiftRight(byte[] data, int shift)
    Shifting all bits in the given data array the given shift to the right.
    static void
    swap(byte[] data, int start, int end, byte[] otherData, int otherStart)
    Swap a given range with a range of the same size with another array.
    static int
    toByteLength(int bitLength)
    Return the minimum number of bytes to store the given number of bits.
    static byte[]
    toBytes(int value)
     
    static byte[]
    toBytes(long value)
     
    static String
    toByteString(byte... data)
    Convert a binary representation of the given byte array to a string.
    static int
    toInt(byte[] data)
     
    static long
    toLong(byte[] data)
     
    static byte[]
    unset(byte[] data, int index)
    Set the bit in the given byte array at the bit position (not the index within the byte array) to false.

    Methods inherited from class java.lang.Object

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

    • get

      public static boolean get(byte[] data, int index)
      Return the (boolean) value of the byte array at the given bit index.
      Parameters:
      data - the byte array.
      index - the bit index.
      Returns:
      the value at the given bit index.
      Throws:
      IndexOutOfBoundsException - if the index is index >= max || index < 0.
      NullPointerException - if the data array is null.
    • set

      public static byte[] set(byte[] data, int index, boolean value)
      Set the bit in the given byte array at the bit position (not the index within the byte array) to the specified value.
      Parameters:
      data - the byte array.
      index - the bit index within the byte array.
      value - the value to set.
      Returns:
      the given data array.
      Throws:
      IndexOutOfBoundsException - if the index is index >= max || index < 0.
      NullPointerException - if the data array is null.
    • set

      public static byte[] set(byte[] data, int index)
      Set the bit in the given byte array at the bit position (not the index within the byte array) to true.
      Parameters:
      data - the byte array.
      index - the bit index within the byte array.
      Returns:
      the given data array.
      Throws:
      IndexOutOfBoundsException - if the index is index >= max || index < 0.
      NullPointerException - if the data array is null.
    • unset

      public static byte[] unset(byte[] data, int index)
      Set the bit in the given byte array at the bit position (not the index within the byte array) to false.
      Parameters:
      data - the byte array.
      index - the bit index within the byte array.
      Returns:
      the given data array.
      Throws:
      IndexOutOfBoundsException - if the index is index >= max || index < 0.
      NullPointerException - if the data array is null.
    • swap

      public static void swap(byte[] data, int start, int end, byte[] otherData, int otherStart)
      Swap a given range with a range of the same size with another array.
      start end | | data: +---+---+---+---+---+---+---+---+---+---+---+---+ +---------------+ +---------------+ otherData: +---+---+---+---+---+---+---+---+---+---+---+---+ | otherStart
      Parameters:
      data - the first byte array which are used for swapping.
      start - the start bit index of the data byte array, inclusively.
      end - the end bit index of the data byte array, exclusively.
      otherData - the other byte array to swap the elements with.
      otherStart - the start index of the otherData byte array.
      Throws:
      IndexOutOfBoundsException - if start > end or if start < 0 || end >= data.length*8 || otherStart < 0 || otherStart + (end - start) >= otherData.length*8
    • count

      public static int count(byte[] data)
      Returns the number of one-bits in the given byte[] array.
      Parameters:
      data - the byte array for which the one bits should be counted.
      Returns:
      the number of one bits in the given byte array.
    • count

      public static int count(byte[] bits, int start, int end)
      Returns the number of one-bits in the given byte[] array.
      Parameters:
      bits - the bit values of the new chromosome gene.
      start - the initial (bit) index of the range to be copied, inclusive
      end - the final (bit) index of the range to be copied, exclusive. (This index may lie outside the array.)
      Returns:
      the number of one-bits in the given byte array.
    • count

      public static int count(byte value)
      Returns the number of one-bits in the given byte value.
      Parameters:
      value - the value for which the one bits should be counted.
      Returns:
      the number of one bits in the given value
    • shiftRight

      public static byte[] shiftRight(byte[] data, int shift)
      Shifting all bits in the given data array the given shift to the right. The bits on the left side are filled with zeros.
      Parameters:
      data - the data bits to shift.
      shift - the number of bits to shift.
      Returns:
      the given data array.
      Throws:
      NullPointerException - if the data array is null.
    • shiftLeft

      public static byte[] shiftLeft(byte[] data, int shift)
      Shifting all bits in the given data array the given shift to the left. The bits on the right side are filled with zeros.
      Parameters:
      data - the data bits to shift.
      shift - the number of bits to shift.
      Returns:
      the given data array.
      Throws:
      NullPointerException - if the data array is null.
    • increment

      public static byte[] increment(byte[] data)
      Increment the given data array.
      Parameters:
      data - the given data array.
      Returns:
      the given data array.
      Throws:
      NullPointerException - if the data array is null.
    • invert

      public static byte[] invert(byte[] data)
      Invert the given data array.
      Parameters:
      data - the given data array.
      Returns:
      the given data array.
      Throws:
      NullPointerException - if the data array is null.
    • complement

      public static byte[] complement(byte[] data)
      Make the two's complement of the given data array.
      Parameters:
      data - the given data array.
      Returns:
      the given data array.
      Throws:
      NullPointerException - if the data array is null.
    • flip

      public static byte[] flip(byte[] data, int index)
      Flip the bit at the given index.
      Parameters:
      data - the data array.
      index - the index of the bit to flip.
      Returns:
      the input array, for command chaining
      Throws:
      IndexOutOfBoundsException - if the index is index >= max || index < 0.
      NullPointerException - if the data array is null.
    • reverse

      public static byte[] reverse(byte[] array)
    • copy

      public static byte[] copy(byte[] data, int start, int end)
      Copies the specified range of the specified array into a new array.
      Parameters:
      data - the bits from which a range is to be copied
      start - the initial index of the range to be copied, inclusive
      end - the final index of the range to be copied, exclusive.
      Returns:
      a new array containing the specified range from the original array
      Throws:
      ArrayIndexOutOfBoundsException - if start < 0 or start > data.length*8
      IllegalArgumentException - if start > end
      NullPointerException - if the data array is null.
    • getAndSet

      public static boolean getAndSet(byte[] array, int index)
    • toByteString

      public static String toByteString(byte... data)
      Convert a binary representation of the given byte array to a string. The string has the following format:
      Byte: 3 2 1 0 | | | | Array: "11110011|10011101|01000000|00101010" | | | | Bit: 23 15 7 0
      Only the array string is printed.
      Parameters:
      data - the byte array to convert to a string.
      Returns:
      the binary representation of the given byte array.
      See Also:
    • fromByteString

      public static byte[] fromByteString(String data)
      Convert a string which was created with the toByteString(byte...) method back to an byte array.
      Parameters:
      data - the string to convert.
      Returns:
      the byte array.
      Throws:
      IllegalArgumentException - if the given data string could not be converted.
      See Also:
    • newArray

      public static byte[] newArray(int length)
      Create a new byte[] array which can store at least the number of bits as defined by the given length parameter.
      Parameters:
      length - the number of bits, the returned byte array can store.
      Returns:
      the new byte array.s
    • newArray

      public static byte[] newArray(int length, double p)
      Create a new byte[] array which can store at least the number of bits as defined by the given length parameter. The returned byte array is initialized with ones according to the given ones probability p.
      Parameters:
      length - the number of bits, the returned byte array can store.
      p - the ones probability of the returned byte array.
      Returns:
      the new byte array.s
      Throws:
      IllegalArgumentException - if p is not a valid probability.
    • toByteLength

      public static int toByteLength(int bitLength)
      Return the minimum number of bytes to store the given number of bits.
      Parameters:
      bitLength - the number of bits
      Returns:
      the number of bytes needed to store the given number of bits.
    • toInt

      public static int toInt(byte[] data)
    • toBytes

      public static byte[] toBytes(int value)
    • toLong

      public static long toLong(byte[] data)
    • toBytes

      public static byte[] toBytes(long value)