I am looking at a C # library called BitStream , which allows you to write and read any number of bits in a standard C # Streamobject. I noticed what seemed like a strange design decision:
When adding bits to an empty byte, bits are added to the MSB byte. For example:
var s = new BitStream();
s.Write(true);
Debug.Assert(s.ToByteArray()[0] == 0x80); // and not 0x01
var s = new BitStream();
s.Write(0x7,0,4);
s.Write(0x3,0,4);
Debug.Assert(s.ToByteArray()[0] == 0x73); // and not 0x37
However, when accessing bits in a number as input, the first bit of the input number is LSB. for example
//s.Write(int input,int bit_offset, int count_bits)
//when referencing the LSB and the next bit we'll write
s.Write(data,0,2); //and not s.Write(data,data_bits_number,data_bits_number-2)
It seems inconsistent to me. Since in this case, by "gradual" copying of the byte, as in the previous example (the first four bits, and then the last four bits), we will not get the original byte. We need to copy it back (first, the last four bits, then the first four bits).
, ? ? ?
, ffmpeg , . , OR src put_bits.
:
, .
var s = new BitStream();
s.Write(0x1,0,4);
s.Write(0x2,0,4);
s.Write(0x3,0,4);
Debug.Assert(s.ToByteArray()[0] == 0x12); // and not s.ToByteArray()[1] == 0x12