private byte ReadBit() { byte result = (byte)((curByte & (1 << (7 - bitPos))) >> (7 - bitPos)); if (++bitPos > 7) { bitPos = 0; try { curByte = io.ReadByte(); } catch (Exception) { curByte = 0; //no more data, read 0 } } return result; }