Archived

This topic is now archived and is closed to further replies.

Stardust

having two separate values in one long

Recommended Posts

Hi! (C++) I have a long and I would like to store one 4 bit value and one 16bit value in it and then be able to get the two values back by just reading the bits. how would I do this? something like this I guess... char cValue = 50; int iValue = 12000; long lValue; // the & is just for showing, I''m trying to find the // specific character to use but I havn''t found it... lValue&31 = cValue&3; lValue&30 = cValue&2; lValue&29 = cValue&1; lValue&28 = cValue&0; lValue&15 = iValue&15; ... what is the character to use? or is there a better method to do this? thanks!

Share this post


Link to post
Share on other sites
Don''t know how you''re reading-in the long, but depending on where the bytes align something like this could work:

  
union PackedLong
{
unsigned long l;
struct
{
unsigned long v4 : 4;
unsigned long v16 : 16;
};
};

You can pack the unused bits with unused bitfield lengths.

Share this post


Link to post
Share on other sites