BitBufferHelper { public static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits) { int numBytes = inputBytes.length; int numBitstoShift = numBits % NetUtils.NUM_BITS_IN_A_BYTE; byte[] shiftedBytes = new byte[numBytes]; int inputLsb = 0; int inputMsb = 0; if (numBitstoShift == 0) { return inputBytes; } for (int i = 1; i < numBytes; i++) { inputLsb = inputBytes[i - 1] & getLSBMask(NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift); inputLsb = inputLsb < 0 ? inputLsb + 256 : inputLsb; inputMsb = inputBytes[i] & getMSBMask(numBitstoShift); inputMsb = inputBytes[i] < 0 ? inputBytes[i] + 256 : inputBytes[i]; shiftedBytes[i] = (byte) (inputLsb << numBitstoShift | inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift); } inputMsb = inputBytes[0] & getMSBMask(numBitstoShift); inputMsb = inputMsb < 0 ? inputMsb + 256 : inputMsb; shiftedBytes[0] = (byte) (inputMsb >> NetUtils.NUM_BITS_IN_A_BYTE - numBitstoShift); return shiftedBytes; } private  BitBufferHelper(); static byte getByte(byte[] data); static short getShort(byte[] data, int numBits); static short getShort(byte[] data); static int getInt(byte[] data, int numBits); static int getInt(byte[] data); static long getLong(byte[] data, int numBits); static long getLong(byte[] data); static byte[] getBits(byte[] data, int startOffset, int numBits); static void setByte(byte[] data, byte input, int startOffset, int numBits); static void setBytes(byte[] data, byte[] input, int startOffset, int numBits); static int getMSBMask(int numBits); static int getLSBMask(int numBits); static long toNumber(byte[] array); static long toNumber(@Nonnull byte[] array, int numBits); static byte[] toByteArray(Number input); static byte[] toByteArray(Number input, int numBits); static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits); static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits); static void insertBits(byte[] data, byte[] inputdataLSB, int startOffset, int numBits); static void checkExceptions(byte[] data, int startOffset, int numBits); static final long BYTE_MASK; }
@Test public void testShiftBitstoLSB() { byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; byte[] data2 = { 8, 9, 10 }; byte[] shiftedBytes2 = BitBufferHelper.shiftBitsToLSB(data2, 11); Assert.assertTrue(shiftedBytes2[0] == 0); Assert.assertTrue(shiftedBytes2[1] == 64); Assert.assertTrue(shiftedBytes2[2] == 72); byte[] shiftedBytes = BitBufferHelper.shiftBitsToLSB(data, 49); Assert.assertTrue(shiftedBytes[0] == 0); Assert.assertTrue(shiftedBytes[1] == 2); Assert.assertTrue(shiftedBytes[2] == 4); Assert.assertTrue(shiftedBytes[3] == 6); Assert.assertTrue(shiftedBytes[4] == 8); Assert.assertTrue(shiftedBytes[5] == 10); Assert.assertTrue(shiftedBytes[6] == 12); Assert.assertTrue(shiftedBytes[7] == 14); Assert.assertTrue(shiftedBytes[8] == 16); Assert.assertTrue(shiftedBytes[9] == 18); byte[] data1 = { 1, 2, 3 }; byte[] shiftedBytes1 = BitBufferHelper.shiftBitsToLSB(data1, 18); Assert.assertTrue(shiftedBytes1[0] == 0); Assert.assertTrue(shiftedBytes1[1] == 4); Assert.assertTrue(shiftedBytes1[2] == 8); }