public void AddByteThenReadNibble()
 {
     var sink = new BitSink();
     sink.AddByte(0xFE);
     Assert.IsTrue(sink.CanRead(4));
     Assert.AreEqual(0xE, sink.Read(4));
     Assert.IsTrue(sink.CanRead(4));
     Assert.AreEqual(0xF, sink.Read(4));
     Assert.IsFalse(sink.CanRead(4));
 }
        public static IEnumerable<byte> PackBytes(IEnumerable<byte> sourceBytes, int bits)
        {
            var sink = new BitSink();

            foreach (byte sourceByte in sourceBytes)
            {
                sink.AddBits(sourceByte, bits);

                while (sink.CanRead(8))
                {
                    yield return sink.Read(8);
                }
            }

            if (!sink.IsEmpty) yield return sink.ReadRemaining();
        }
        public static IEnumerable<byte> UnpackBytes(IEnumerable<byte> sourceBytes, int bits, bool discardRemainder)
        {
            var sink = new BitSink();

            foreach (byte sourceByte in sourceBytes)
            {
                sink.AddByte(sourceByte);

                while (sink.CanRead(bits))
                {
                    yield return sink.Read(bits);
                }
            }

            if (!(sink.IsEmpty || discardRemainder))
                yield return sink.ReadRemaining();
        }