Class Bits
java.lang.Object
net.tangly.commons.utilities.Bits
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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.
-
Field Details
-
BYTE
public static final int BYTESize of a byte in bits.- See Also:
-
WORD
public static final int WORDSize of a word in bits.- See Also:
-
DWORD
public static final int DWORDSize of a double word in bits.- See Also:
-
-
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 nulldata
- byte of data to be inserted into the bit streamposition
- start index in bits in the stream, starting at 0length
- 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 nullposition
- start index in bits in the stream, starting at 0length
- 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 nulldata
- word of data to be inserted into the bit streamposition
- start index in bits in the stream, starting at 0length
- 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 nullposition
- start index in bits in the stream, starting at 0length
- 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 nulldata
- long word of data to be inserted into the bit streamposition
- start index in bits in the stream, starting at 0length
- 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 nullposition
- start index in bits in the stream, starting at 0length
- number of bits to insert into the stream. The format is constrained in [1..32]- Returns:
- the extracted data from the byte stream
-
toHex
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
-