Marshaling data in Bit

To reduce the amount of data being saved message, you can marshal data in bit during

Proud.CMessage has methods that can save data in bit as listed in below.

  • Read from bit: Proud.CMessage.ReadBits
  • Write in bit: Proud.CMessage.WriteBits

Here is an example.

namespace Proud
{
struct MyType
{
int x,y,z;
};
CMessage& operator>>(CMessage &a, MyType &b)
{
// Use 6 bits to read x value
a.ReadBits(b.x,6);
// Use 3 bits to read y value
a.ReadBits(b.y,3);
// Use 15 bits to read z value. The total bits used for reading is 6+3+15=24 bits (3 bytes)
a.ReadBits(b.z,15);
return a;
}
CMessage& operator<<(CMessage &a, const MyType &b)
{
a.WriteBits(b.x,6); // Save x,y and z in bit.
a.WriteBits(b.y,3);
a.WriteBits(b.z,15);
return a;
}
}

Things you need to be careful about as marshaling data in bit.

The number of bits that you want to use for saving should not exceed the range of actual value. For example, let's say there is an int which has negative value. Then the first bit of that int will be 1. So if you push to save it with less than 31 bits to reduce the amount of bits, you would end up losing the first bit value. Thus you need to be cautious as using read/write in bits.