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 void AddBytesThenReadBytes()
        {
            var sink = new BitSink();

            sink.AddByte(0xFF);
            sink.AddByte(0x00);
            sink.AddByte(0x0F);
            sink.AddByte(0xF0);
            sink.AddByte(0xFF);

            Assert.AreEqual(0xFF, sink.Read(8));
            Assert.AreEqual(0x00, sink.Read(8));
            Assert.AreEqual(0x0F, sink.Read(8));
            Assert.AreEqual(0xF0, sink.Read(8));
            Assert.AreEqual(0xFF, sink.Read(8));
        }
        public void AddNiblesThenReadByte()
        {
            var sink = new BitSink();

            sink.AddBits(0xE, 4);
            sink.AddBits(0xF, 4);
            Assert.AreEqual(0xFE, sink.Read(8));
        }
        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());
            }
        }
        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());
            }
        }