Jump to content
  • Advertisement
Sign in to follow this  
Eitsch

Serialize - data packing

This topic is 4490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

let's say i have 2 floats. i want to pack them into one big unit (not an array or vector).

Share this post


Link to post
Share on other sites
Advertisement
c/c++ :

union U
{
struct {
float f1,f2;
};
double d;
};

...

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



Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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'

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!