Original Post
let's say i have 2 floats. i want to pack them into one big unit (not an array or vector).
union U{ struct { float f1,f2; }; double d;};...U u;u.f1 = your_float1;u.f2 = your_float2;your_double = u.d;double PackFloats(float f1, float f2){ return ((double)f1 << 32) + ((double)f2);}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.
struct MyStruct{ float f1; float f2;}MyStruct ExtractDouble(double tempD){ MyStruct returnS; returnS.f2 = tempD & 4294967295; returnS.f1 = (tempD >> 32) & 4294967295; return returnS;}Quote:
Original post by dbzprogrammer
double PackFloats(float f1, float f2)
{
return ((double)f1 << 32) + ((double)f2);
}
Quote:
Original post by Cocalus
Is basically filling your_double with meaningless bits.
#include <iostream>using namespace std;union U{ struct { float f1,f2; }; double d;};int main(){ //Serialize U u; u.f1 = 2.8; u.f2 = 3.1; double d = u.d; //Deerialize U u2; u2.d = d; float f1 = u2.f1; float f2 = u2.f2; cout << f1 << endl << f2 << endl;}/*Output:2.83.1*/void PutFloat( std::vector<char>& vec, float data ){ vec.insert( vec.end(), reinterpret_cast<char const*>( &data ), reinterpret_cast<char const*>( &data + 1 ) );}float GetFloat( std::vector<char> const& vec, std::size_t index ){ if( index < index + sizeof(float) && index + sizeof(float) <= vec.size() ) { float val = *reinterpret_cast<float const*>( &vec[index] ); return val; } //access would overflow vector boundaries - throw something? return 0.0f;}Quote:
Original post by Kambiz
It is confusing and is it not needed he can use unsigned __int64 too(better than the double)... but he asked how to do it and I just answered.
Quote:
Original post by Eitsch
...not an array or vector...
Quote:
Original post by RDragon1
If you want to serialize it to something like a vectorbuffer,...
Quote:
Original post by Kambiz
Quote: Original post by Eitsch
...not an array or vector...
Quote: Original post by RDragon1
If you want to serialize it to something like a vectorbuffer,...
Quote:
Original post by RDragon1
That just raises the question - why not a vector?
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more