* @param int startOffset - offset to start fetching bits from data from
* @param int numBits - number of bits to be fetched from data
* @return byte [] - LSB aligned bits
- *
+ *
* @throws BufferException
* when the startOffset and numBits parameters are not congruent
* with the data buffer size
// Now adding the rest of the bits if any
if (extranumBits != 0) {
if (extranumBits < (NetUtils.NumBitsInAByte - extraOffsetBits)) {
- valfromnext = (byte) (data[startByteOffset + i + 1] & ((getMSBMask(extranumBits)) >> extraOffsetBits));
+ valfromnext = (byte) (data[startByteOffset + i] & ((getMSBMask(extranumBits)) >> extraOffsetBits));
bytes[i] = (byte) (valfromnext << extraOffsetBits);
} else if (extranumBits == (NetUtils.NumBitsInAByte - extraOffsetBits)) {
valfromcurr = (data[startByteOffset + i])
* @param byte - input byte to be inserted
* @param startOffset - offset of data[] to start inserting byte from
* @param numBits - number of bits of input to be inserted into data[]
- *
+ *
* @throws BufferException
* when the input, startOffset and numBits are not congruent
* with the data buffer size
/**
* Returns numBits 1's in the MSB position
- *
+ *
* @param numBits
* @return
*/
/**
* Returns numBits 1's in the LSB position
- *
+ *
* @param numBits
* @return
*/
/**
* Returns the numerical value of the byte array passed
- *
+ *
* @param byte[] - array
* @return long - numerical value of byte array passed
*/
/**
* Returns the numerical value of the last numBits (LSB bits) of the byte
* array passed
- *
+ *
* @param byte[] - array
* @param int - numBits
* @return long - numerical value of byte array passed
* Accepts a number as input and returns its value in byte form in LSB
* aligned form example: input = 5000 [1001110001000] bytes = 19, -120
* [00010011] [10001000]
- *
+ *
* @param Number
* @return byte[]
- *
+ *
*/
public static byte[] toByteArray(Number input) {
* Accepts a number as input and returns its value in byte form in MSB
* aligned form example: input = 5000 [1001110001000] bytes = -114, 64
* [10011100] [01000000]
- *
+ *
* @param Number
* input
* @param int numBits - the number of bits to be returned
* @return byte[]
- *
+ *
*/
public static byte[] toByteArray(Number input, int numBits) {
Class<? extends Number> dataType = input.getClass();
/**
* Takes an LSB aligned byte array and returned the LSB numBits in a MSB
* aligned byte array
- *
+ *
* @param inputbytes
* @param numBits
* @return
/**
* It aligns the last numBits bits to the head of the byte array following
* them with numBits % 8 zero bits.
- *
+ *
* Example: For inputbytes = [00000111][01110001] and numBits = 12 it
* returns: shiftedBytes = [01110111][00010000]
- *
+ *
* @param byte[] inputBytes
* @param int numBits - number of bits to be left aligned
* @return byte[]
/**
* It aligns the first numBits bits to the right end of the byte array
* preceding them with numBits % 8 zero bits.
- *
+ *
* Example: For inputbytes = [01110111][00010000] and numBits = 12 it
* returns: shiftedBytes = [00000111][01110001]
- *
+ *
* @param byte[] inputBytes
* @param int numBits - number of bits to be right aligned
* @return byte[]
* Insert in the data buffer at position dictated by the offset the number
* of bits specified from the input data byte array. The input byte array
* has the bits stored starting from the LSB
- *
+ *
* @param byte[] data
* @param byte[] inputdata
* @param int startOffset
int InputMSBbits = 0, InputLSBbits = 0;
int i;
- if (numBits == 0) {
- return;
+ if (numBits == 0) {
+ return;
}
if (extraOffsetBits == 0) {