Java tutorial
//package com.java2s; public class Main { public static boolean getNext(int index, byte[] data) { return ((data[getBytePos(index)] >> getBitPos(index)) & 1) == 1; } public static long getNext(int index, byte[] data, final int offset) { long result = 0; for (int lengthRemain = offset, start = getBitPos(index); lengthRemain != 0; start = 0) { int end = Math.min(lengthRemain + start - 1, 5); int length = end - start + 1; for (int i = 0; i < length; i++, index++) result = (result << 1) + (getNext(index, data) ? 1 : 0); lengthRemain -= length; } return result; } public static int getBytePos(int index) { return index / 6; } private static int getBitPos(int index) { return index % 6; } }