public ulong GetInteger(int BitLength)
{
ulong outval = 0L;
int rshift, lshift;
byte[] v = GetNext(BitLength);
if (BitOffset == 0) { // landed on a byte-boundary
rshift = 0;
} else {
rshift = 8 - BitOffset;
}
lshift = 8 - rshift;
// This mess shifts the bits of a byte array by up to 8 places
// and reverses byte order, joining the result into a ulong.
int psh = 0;
for (int i = v.Length - 1; i >= 0; i--) {
int n0 = v[i] >> rshift;
int n1 = (i > 0) ? (v[i-1] << lshift) : (0);
outval += (ulong)(((n0 + n1) & 0xFF) << psh);
psh += 8;
}
return outval;
}