public class ByteBitBuffer { /// /// The binary buffer. /// private readonly byte[] bytes; /// /// Initializes a new instance of the class. /// /// The length of the buffer. public ByteBitBuffer(int length) { var len = length / 8; if(length % 8 != 0) { len++; } this.bytes = new byte[len]; } /// /// Sets the value at an index. /// /// The index. /// The value. public void Set(int index, bool value) { byte mask = (byte)(1 << (index % 8)); if(value) { this.bytes[index / 8] |= mask; } else { this.bytes[index / 8] &= (byte)~mask; } } /// /// Gets the value at an index. /// /// The index. /// The value. public bool Get(int index) { return ((this.bytes[index / 8] >> (index % 8)) & 1) != 0; } }