Reading the first n bits of a byte
I have not tested this that well. If anyone finds a bug, please let me know.

// Throws away the unused bits
public byte ReadFirstNBits(int n)
{
    Contract.Requires(n &gt;= 0 &amp;&amp; n &lt;= 8, "Code contract may be too slow for your needs");
    byte val = base.ReadByte();
    return val &gt;&gt; (8 - n);
}

// Throws away the unused bits
public byte ReadLastNBits(int n)
{
    Contract.Requires(n &gt;= 0 &amp;&amp; n &lt;= 8, "Code contract may be too slow for your needs");
    byte val = base.ReadByte();
    byte mask = (byte)((1 &lt;&lt; n) - 1);
    return val &amp; mask;
}


You can try this for yourself if you have a Python prompt:

&gt;&gt;&gt; a = 0b11001001 
&gt;&gt;&gt; a
201
&gt;&gt;&gt; a &gt;&gt; 4
12
&gt;&gt;&gt; a &gt;&gt; 5
6
&gt;&gt;&gt; a &gt;&gt; 3
25
&gt;&gt;&gt; a &gt;&gt; 6
3
&gt;&gt;&gt; a &gt;&gt; 2
&gt;&gt;&gt; for i in range(8): print bin(a &gt;&gt; i)

0b11001001
0b1100100
0b110010
0b11001
0b1100
0b110
0b11
0b1
&gt;&gt;&gt;