Class Bits

java.lang.Object
net.tangly.commons.utilities.Bits

public final class Bits extends Object

The bit utility class provides utility functions to manipulate bit streams as used on older communication protocols. The functions manipulate byte (8 bits), short (16 bits) and long (32 bits) bit streams and append them into the bit stream at location. Mirror functions support the extraction of bits from a bit stream.

Any legacy bit oriented communication protocols can efficiently be handled with the provided functions.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Size of a byte in bits.
    static final int
    Size of a double word in bits.
    static final int
    Size of a word in bits.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    appendBits(byte[] stream, byte data, int position, int length)
    Writes length number of bits from data into the byte stream at the bit position.
    static int
    appendBits(byte[] stream, int data, int position, int length)
    Writes length number of bits from data into the byte stream at the bit position.
    static int
    appendBits(byte[] stream, short data, int position, int length)
    Writes length number of bits from data into the byte stream at the bit position.
    static byte
    extractBitsToByte(byte[] stream, int position, int length)
    Extracts length number of bits from the byte stream at the bit position.
    static int
    extractBitsToInt(byte[] stream, int position, int length)
    Extracts length number of bits from the byte stream starting at the bit position.
    static short
    extractBitsToShort(byte[] stream, int position, int length)
    Extracts length number of bits from the byte stream at the bit position.
    static String
    toHex(byte[] stream)
    Transforms the stream of byte into a human-readable sequence of hexadecimal values.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • appendBits

      public static int appendBits(byte[] stream, byte data, int position, int length)
      Writes length number of bits from data into the byte stream at the bit position. The assumption is that bits are appended and the stream is initialized to 0.
      Parameters:
      stream - stream of bits encoded as an array of bytes. The stream cannot be null
      data - byte of data to be inserted into the bit stream
      position - start index in bits in the stream, starting at 0
      length - number of bits to insert into the stream. The format is constrained in [1..8]
      Returns:
      the position at the end of the written stream
    • extractBitsToByte

      public static byte extractBitsToByte(byte[] stream, int position, int length)
      Extracts length number of bits from the byte stream at the bit position.
      Parameters:
      stream - stream of bits encoded as an array of bytes. The stream cannot be null
      position - start index in bits in the stream, starting at 0
      length - number of bits to insert into the stream. The format is constrained in [1..8]
      Returns:
      the extracted data from the byte stream
    • appendBits

      public static int appendBits(byte[] stream, short data, int position, int length)
      Writes length number of bits from data into the byte stream at the bit position. The assumption is that bits are appended and the stream is initialized to 0.
      Parameters:
      stream - stream of bits encoded as an array of bytes. The stream cannot be null
      data - word of data to be inserted into the bit stream
      position - start index in bits in the stream, starting at 0
      length - number of bits to insert into the stream. The format is constrained in [1..16]
      Returns:
      the position at the end of the written stream
    • extractBitsToShort

      public static short extractBitsToShort(byte[] stream, int position, int length)
      Extracts length number of bits from the byte stream at the bit position.
      Parameters:
      stream - stream of bits encoded as an array of bytes. The stream cannot be null
      position - start index in bits in the stream, starting at 0
      length - number of bits to insert into the stream. The format is constrained in [1..16]
      Returns:
      the extracted data from the byte stream
    • appendBits

      public static int appendBits(byte[] stream, int data, int position, int length)
      Writes length number of bits from data into the byte stream at the bit position. The assumption is that bits are appended and the stream is initialized to 0.
      Parameters:
      stream - stream of bits encoded as an array of bytes. The stream cannot be null
      data - long word of data to be inserted into the bit stream
      position - start index in bits in the stream, starting at 0
      length - number of bits to insert into the stream. The format is constrained in [1..32]
      Returns:
      the position at the end of the written stream
    • extractBitsToInt

      public static int extractBitsToInt(byte[] stream, int position, int length)
      Extracts length number of bits from the byte stream starting at the bit position.
      Parameters:
      stream - stream of bits encoded as an array of bytes. The stream cannot be null
      position - start index in bits in the stream, starting at 0
      length - number of bits to insert into the stream. The format is constrained in [1..32]
      Returns:
      the extracted data from the byte stream
    • toHex

      public static String toHex(byte[] stream)
      Transforms the stream of byte into a human-readable sequence of hexadecimal values.
      Parameters:
      stream - the byte stream to transform. The stream must not be null
      Returns:
      the hexadecimal representation of the byte stream