Bit streams in C++

Reading and writing specific bits is a common task. This article reviews
encountered methods and introduces C++ metaprogramming-style one. There are a
few conventional techniques to do that:

Apply bitwise operations manually — tedious and efficient

Use bit fields —
efficient, less tedious, but confusing and hardly portable across platforms
with different endianness

Create a custom class that would treat bit streams in run time — there are
lots of
examples in
the web.

Here is another way, which is as efficient as manual code, but superb in terms
of convenience. Let the compiler know in advance as much as possible, and it
will be able to generate specific optimized code. Consider the source file
BitStream.cpp.
It defines two template functions:

// Read 'count' number of bits from a memory starting at bit 'offset'// The compiler will easily optimize the implementation.template<unsignedoffset,unsignedcount>inlineuint32_tRead(constuint8_t*src,uint32_taccum=0);// Write given value to the memory starting at 'offset' bit// spanning 'count' bitstemplate<unsignedoffset,unsignedcount>inlinevoidWrite(uint8_t*dst,uint32_tvalue);

For each combination of parameters offset and count, a specific function is
generated, which is merely few instructions long. And they are natural to use,
for example, consider parsing RTP header: