gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Saving data in a 32 bit integer

#47404 - QuantumDoja - Fri Jul 08, 2005 4:29 pm

Hi, please bear with me as i try to explain.

I have a cube, and I want to store 4 things about the cube,
Its x Length,
Its y Length,
Its z length,
Its ID.

I could save this information as (for example) "15,15,15,1" so "x,y,z,ID"

but, Would it be better to stuff the information into a 32 bit integer, thus reducing the file size???

as in, it would then become:
Code:

00001111000011110000111100000001

and i could save that in a 32 bit integer and take out the bits i required?? Whereas saving as "15,15,15,1" i would have to use 4 variables?
_________________
Chris Davis

#47406 - Ultima2876 - Fri Jul 08, 2005 4:36 pm

As far as I know, there's enough space in RAM and especially ROM that this wouldn't really be necessary, and would waste CPU time extracting the right bits and performing the caclulations needed to get the original values and update the old ones. Plus, the coding would be a tiny bit harder. If you were working with a large array it may be a problem, because the wasted space obviously multiplies up with the array size.

As the greats always say, it's better to save the optimising for when you need to - it'd probably be better to just use the 4 variables seperately for now and waste the few bytes of space, then if you need that space later (which is unlikely) you know what to change.

#47411 - Dwedit - Fri Jul 08, 2005 5:12 pm

Or you could tell the compiler that it's a char array, so it would access each element with normal array indexing.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#47415 - sajiimori - Fri Jul 08, 2005 5:29 pm

Code:

struct Cube
{
  u8 x, y, z, id;
};

There. Now you can replace u8 with u32 or whatever else if you need more range later. ^_^

The important thing is to hide the implementation details that might change. The interface to the cube structure shouldn't be dependent on the sizes of the elements.