//public static ulong ReadUInt64(byte[] fromBuffer, int numberOfBits, int readBitOffset) /// <summary> /// Writes an unsigned 16 bit integer /// </summary> public static void WriteUInt16(ushort source, int numberOfBits, byte[] destination, int destinationBitOffset) { if (numberOfBits == 0) { return; } BitBufferException.Assert((numberOfBits >= 0 && numberOfBits <= 16), "numberOfBits must be between 0 and 16"); #if BIGENDIAN // reorder bytes uint intSource = source; intSource = ((intSource & 0x0000ff00) >> 8) | ((intSource & 0x000000ff) << 8); source = (ushort)intSource; #endif if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset); return; } BitReaderWriter.WriteByte((byte)source, 8, destination, destinationBitOffset); numberOfBits -= 8; if (numberOfBits > 0) { BitReaderWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset + 8); } }
/// <summary> /// Write a byte /// </summary> public BitWriter Write(byte source) { EnsureBufferSize(_lengthBits + 8); BitReaderWriter.WriteByte(source, 8, _data, _lengthBits); _lengthBits += 8; return(this); }
/// <summary> /// Writes a boolean value using 1 bit /// </summary> public BitWriter Write(bool value) { EnsureBufferSize(_lengthBits + 1); BitReaderWriter.WriteByte((value ? (byte)1 : (byte)0), 1, _data, _lengthBits); _lengthBits += 1; return(this); }
/// <summary> /// Writes 1 to 8 bits of a byte /// </summary> public BitWriter Write(byte source, int numberOfBits) { BitBufferException.Assert((numberOfBits > 0 && numberOfBits <= 8), "Write(byte, numberOfBits) can only write between 1 and 8 bits"); EnsureBufferSize(_lengthBits + numberOfBits); BitReaderWriter.WriteByte(source, numberOfBits, _data, _lengthBits); _lengthBits += numberOfBits; return(this); }
/// <summary> /// Writes a byte at a given offset in the buffer /// </summary> public BitWriter WriteAt(Int32 offset, byte source) { int newBitLength = Math.Max(_lengthBits, offset + 8); EnsureBufferSize(newBitLength); BitReaderWriter.WriteByte((byte)source, 8, _data, offset); _lengthBits = newBitLength; return(this); }
/// <summary> /// Writes the specified number of bits into a byte array /// </summary> public static int WriteUInt32(uint source, int numberOfBits, byte[] destination, int destinationBitOffset) { #if BIGENDIAN // reorder bytes source = ((source & 0xff000000) >> 24) | ((source & 0x00ff0000) >> 8) | ((source & 0x0000ff00) << 8) | ((source & 0x000000ff) << 24); #endif int returnValue = destinationBitOffset + numberOfBits; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)source, 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 8), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 16), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 16), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; BitReaderWriter.WriteByte((byte)(source >> 24), numberOfBits, destination, destinationBitOffset); return(returnValue); }
/// <summary> /// Writes the specified number of bits into a byte array /// </summary> public static int WriteUInt64(ulong source, int numberOfBits, byte[] destination, int destinationBitOffset) { #if BIGENDIAN source = ((source & 0xff00000000000000L) >> 56) | ((source & 0x00ff000000000000L) >> 40) | ((source & 0x0000ff0000000000L) >> 24) | ((source & 0x000000ff00000000L) >> 8) | ((source & 0x00000000ff000000L) << 8) | ((source & 0x0000000000ff0000L) << 24) | ((source & 0x000000000000ff00L) << 40) | ((source & 0x00000000000000ffL) << 56); #endif int returnValue = destinationBitOffset + numberOfBits; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)source, 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 8), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 16), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 16), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 24), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 24), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 32), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 32), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 40), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 40), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 48), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 48), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; if (numberOfBits <= 8) { BitReaderWriter.WriteByte((byte)(source >> 56), numberOfBits, destination, destinationBitOffset); return(returnValue); } BitReaderWriter.WriteByte((byte)(source >> 56), 8, destination, destinationBitOffset); destinationBitOffset += 8; numberOfBits -= 8; return(returnValue); }