namespace Febe.Core.Codecs.Containers;
/// A big-endian bit reader over the few bytes of a codec header; past the end it gives zeros.
internal ref struct BitReader(ReadOnlySpan data, int position)
{
private readonly ReadOnlySpan data = data;
private int position = position;
/// The next bits (at most 31) as an unsigned number.
internal int Read(int count)
{
var value = 0;
for (var i = 0; i < count; i++, position++)
{
var index = position >> 3;
var bit = index < data.Length ? (data[index] >> (7 - (position & 7))) & 1 : 0;
value = (value << 1) | bit;
}
return value;
}
}