private static ulong GetBits64(byte[] data, int bitPos, int bitsCount)
{
ulong result = 0;
int currentBytePos = bitPos / 8;
int currentBitInBytePos = bitPos % 8;
int xLength = Math.Min(bitsCount, 8 - currentBitInBytePos);
if (xLength != 0)
{
result = ((ulong)data[currentBytePos] << 56 + currentBitInBytePos) >> 64 - xLength << bitsCount - xLength;
currentBytePos += (currentBitInBytePos + xLength) / 8;
currentBitInBytePos = (currentBitInBytePos + xLength) % 8;
int x2Length = bitsCount - xLength;
if (x2Length > 8)
x2Length = 8;
while (x2Length > 0)
{
xLength += x2Length;
result |= (ulong)data[currentBytePos] >> 8 - x2Length << bitsCount - xLength;
currentBytePos += (currentBitInBytePos + x2Length) / 8;
currentBitInBytePos = (currentBitInBytePos + x2Length) % 8;
x2Length = bitsCount - xLength;
if (x2Length > 8)
x2Length = 8;
}
}
return result;
}