private static BigInteger GetBitsN(byte[] data, int bitPos, int bitsCount)
{
BigInteger result = 0;
int currentBytePos = bitPos / 8;
int currentBitInBytePos = bitPos % 8;
int xLength = Math.Min(bitsCount, 8 - currentBitInBytePos);
if (xLength != 0)
{
result = (((BigInteger)data[currentBytePos] >> 8 - xLength - currentBitInBytePos) & two_in_power_n[7 - currentBitInBytePos]) << 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 |= (BigInteger)(data[currentBytePos] >> 8 - x2Length) << bitsCount - xLength;
currentBytePos += (currentBitInBytePos + x2Length) / 8;
currentBitInBytePos = (currentBitInBytePos + x2Length) % 8;
x2Length = bitsCount - xLength;
if (x2Length > 8)
x2Length = 8;
}
}
return result;
}