public void Write(uint source) { int newLength = bitLength + 32; ExpandBuffer(newLength); if (bitLength % 8 == 0) { unsafe { fixed(byte *numRef = &data[bitLength / 8]) { *((uint *)numRef) = source; } } } else { BitWriter.WriteUInt32(source, 32, data, bitLength); } bitLength = newLength; }
public void Write(int source, int numberOfBits) { int newLength = bitLength + numberOfBits; ExpandBuffer(newLength); if (numberOfBits != 32) { //make first bit sign int signBit = 1 << (numberOfBits - 1); if (source < 0) { source = (-source - 1) | signBit; } else { source &= (~signBit); } } BitWriter.WriteUInt32((uint)source, numberOfBits, data, bitLength); bitLength = newLength; }
public ulong ReadUInt64(int numberOfBits) { int newPosition = bitPosition + numberOfBits; if (overflow || newPosition > endBitPosition) { overflow = true; return(0); } ulong value; if (numberOfBits <= 32) { value = (ulong)BitWriter.ReadUInt32(data, numberOfBits, bitPosition); } else { value = BitWriter.ReadUInt32(data, 32, bitPosition); value |= BitWriter.ReadUInt32(data, numberOfBits - 32, bitPosition) << 32; } bitPosition += numberOfBits; return(value); }