Serialize - data packing

Started by
27 comments, last by Tradone 18 years ago
let's say i have 2 floats. i want to pack them into one big unit (not an array or vector).
Advertisement
struct float2
{
float f1,f2;
};

Where is the problem??
Perhaps you're looking for a double? Set the high 4 bytes to the first float and the next 4 low bytes to the second?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
dbzprogrammer how to do this?
c/c++ :
union U{	struct {		float f1,f2;	};	double d;};...U u;u.f1 = your_float1;u.f2 = your_float2;your_double = u.d;
I understood the concept, and after a quick pull, I double checked my mental code.

Link.

Fundamentals, bits are what makes a byte. The higher the bit in the sequence, the greater the number it represents. Each bit represents a 2^x number. Normally, we would copy byte for byte, using the & (bit-wise AND) with 255 to determine which bits we want. However, we're doing 4 bytes at a time, so we will have to use the number 4294967295. This sets 32 bits to '1's.

So, follow the code carfully:

double PackFloats(float f1, float f2){  return ((double)f1 << 32) + ((double)f2);}


EDIT: Hit the button too soon. The << is an operator that "shifts" bits. Basically, you're telling the computer that you want to fill in 4 bytes of the double with f1, starting at bit 32, and ends up at bit 63, the last bit (remember, we have bit 0 that makes it a total of 64 bits).
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Um, packing two small units into a meaningful larger unit only works with unsigned integers. The last bit of code gives you a structure of either two floats or a double.

u.f1 = your_float1;
u.f2 = your_float2;
your_double = u.d;

Is basically filling your_double with meaningless bits.

The
struct float2{
float f1,f2;
}
is most likely what the OP is looking for.
Quote:Original post by Cocalus
Um, packing two small units into a meaningful larger unit only works with unsigned integers. The last bit of code gives you a structure of either two floats or a double.

u.f1 = your_float1;
u.f2 = your_float2;
your_double = u.d;

Is basically filling your_double with meaningless bits.

The
struct float2{
float f1,f2;
}
is most likely what the OP is looking for.


He has a good point, as floating point precision would get messed up...

But he's just trying to back it up so he could extract it later, would that affect it?
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Let me add to my previous post here.

How would you de-searilize it?

With a function like the one before, except those numbers that I showed you would actually come into play. Observe:

struct MyStruct{  float f1;  float f2;}MyStruct ExtractDouble(double tempD){  MyStruct returnS;  returnS.f2 = tempD & 4294967295;  returnS.f1 = (tempD >> 32) & 4294967295;  return returnS;}
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Quote:Original post by dbzprogrammer
double PackFloats(float f1, float f2)
{
return ((double)f1 << 32) + ((double)f2);
}

This works with interegs but not with doubles:
error C2296: '<<' : illegal, left operand has type 'double'

This topic is closed to new replies.

Advertisement