The way I am using this method assumes all data is byte-aligned, so I don't really care about the rest of the bits.
This method is written in an extension of BinaryReader which provides more methods when I'm reading binary data from files. The BinaryReader provided does not appear to support reading bits.
Is there a better way I could write this method? Also I'm not sure if there's a more direct way to build a bitmask so I did it the long way.
/* Returns the value of the first n bits. */
public byte ReadBits(byte n)
{
byte val = base.ReadByte();
byte sum = 0;
for (int i = 0; i < n; i++)
sum += (byte)(val & (1 << i));
return sum;
}
I am using .NET 3.5