CBitBuffer Class - Data Serialization in MQL5 - MetaTrader 5 Library - MT4/MT5 Resources

The CBitBuffer class provides a solid basis for bit-level data serialization in MQL5, providing fine-grained control over data storage and retrieval. This class supports various data types, including variable length integers (VLQ with ZigZag encoding) as well as serialization of strings and structures, which is useful for optimizing space. This class employs optimizations such as internal buffering and exponential array growth to enhance performance and provides a comprehensive error handling system. It is particularly useful for network communications or file storage, where minimizing data size is critical (e.g., compressing tick data).

//+------------------------------------------------------------------+ //| CBitBuffer class | //|Read/write individual bits or bit sequences to the ulong[] buffer. | //|Supports efficient bit operations and mixed read/write operations. | //+------------------------------------------------------------------+ class CBitBuffer { public : // Core bit manipulation boolean writebits( boolean_small_number ); // write a one-bit boolean write_bit( own_value , integer_number ); // write N bits of boolean from ulong read_bits(); // read a bit of boolean read_bit( integer_number ); // read N bits as ulong own_peep_bits ( integer_number ); // read N bits without advancing the position public : // position and size management boolean set read position (bit position long ); // set read position (bit position long ) get read position(); boolean reset read position(); // reset read position to 0 boolean skip bits ( long skip bits); // skip N bits from current read position get total writtenBits(); // total number of bits written long get total bytes written (); // total number of bytes written long get total bytes allocated (); // total number of bytes allocated long get remaining read bits (); // remaining bits to read public : // data type specific read/write operations boolean writeboolean( boolean value); boolean writeChar( charValue ); Boolean value written to UChar( Uchar value); boolean writeShort( short's value); Boolean value write UShort( ultrashort value); boolean writeInt( integerValue ); Boolean value Write UInt( unit value); boolean writeLong( long's value); Boolean value is written to ULong( Oolong value); boolean writeDateTime( DateTimeValue ); boolean write float( float value); // write a 32-bit float boolean write double( double the value); // write a 64-bit double boolean write string( string value); // write a string template with length prefix < type name > Boolean value WriteStruct(T &struct_object); // Write a structure Boolean value with length prefix ReadBool(); Character ReadChar(); Uchar ReadUChar(); Short ReadShort(); Ultra short ReadUShort(); Integer ReadInt(); Unit ReadUInt(); long readLength(); Oolong ReadULong(); DateTime ReadDateTime(); Float ReadFloat(); // Read a 32-bit float double ReadDouble(); // Read a 64-bit double StringReadString(); // Read a length-prefixed string template < type name > T ReadStruct(); // Read a length-prefixed structure public : // Variable Length Encoding (VLQ) of an integer boolean WriteVarInt( integer value); // Use ZigZag + VLQ Write signed int boolean WriteVarUInt( unit value); // Use VLQ to write unsigned int boolean Write VarLong( long value); // Use ZigZag + VLQ to write signed long boolean value Write VarULong( own value); // Use VLQ to write unsigned long integer ReadVarInt(); // Use ZigZag + VLQ to read signed int unit ReadVarUInt(); // Use VLQ to read unsigned int long ReadVarLong(); // Use ZigZag + VLQ to read signed long integer ReadVarULong (); // Use VLQ to read unsigned long : // Buffer management blank clear(); // Clear buffer content and reset status boolean to get final buffer ( oolong &destinationArray[]); // Copy buffer contents to array boolean set original buffer( const oo &source_buffer[]); // Set buffer contents from array boolean save( string filename); // Save buffer to file boolean load( string filename); // Load buffer from file boolean : // Error handling ENUM_BIT_BUFFER_ERROR get last error (); // Return last error code string get last error string(); // return error description string blank clear last error(); // clear last error blank print hex(); // print main buffer in hex (debug) };
Complete test example " CBitBuffer_Test.mq5" provided above is the best way to detect errors and verify class functionality.
The header file " short-ticks.mqh " provides an alternative implementation of ' short-tick ' fxsaber's tick compression library, utilizing VLQ encoding in the "CBitBuffer" class.
2025.07.21 - v.1.01:
2025.07.22 - v.1.02:
Attachment download
📎CBitBuffer.mqh (95.3 KB)
📎CBitBuffer_Test.mq5 (39.12 KB)
📎TicksShort.mqh (14.12 KB)
📎TicksShort_SaveLoad.mq5 (2 KB)
📎TicksShort_Performance.mq5 (4.69 KB)
📎TicksShortLite.mqh (17.16 KB)
Source: MQL5 #61728
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •