A Bite-Sized BitArray

The type wrapping the [UInt8] should keep track of how many bits of the final UInt8 are actually being used. The unused bits' values shouldn't matter, because your Sequence/Collection interface should never vend that part of the final byte.

For your Index, it should be like an (Array<UInt8>.Index, UInt8) tuple. The first member is the byte with the targeted bit; the second member is a mask referencing the position of the targeted bit. The endIndex would be (myBytes.endIndex, 1) if the bit elements take up the exact amount of space. This is OK since you should never be dereferencing it anyway.

1 Like