Packing Bytes

Hi,

The literature I'm reading suggests that I can specify the number of bits a certain data type must occupy, and that I can possibly store several of these packed variables into the same byte for reduced storage space.

typedef struct{
unsigned int a: 4;
unsigned char b: 2;
} my_type;

I tried the above but it just truncates the values assigned and stores them in separate two byte wide and 1 byte wide spaces respectively. What's the trick?

Regards,
Murray R. Van Luyn.

Parents
  • What's the trick?

    The trick? Try to learn your profession before doing it. The hack-and-slash technique might have worked when learning the chimp-designed deep fryers when you were at McDonald's, but not so well as an embedded programmer. Please read the manual.

    Oh yeah... and avoid jokes that throw back to a time of hardship for the entire civilized world to get a cheap laugh based on the ethnicity of a company name.

    For those of you following along at home: This gentlemen posted the same question in another forum where he referred to the compiler as "Siege Heil C" (his spelling).

Reply
  • What's the trick?

    The trick? Try to learn your profession before doing it. The hack-and-slash technique might have worked when learning the chimp-designed deep fryers when you were at McDonald's, but not so well as an embedded programmer. Please read the manual.

    Oh yeah... and avoid jokes that throw back to a time of hardship for the entire civilized world to get a cheap laugh based on the ethnicity of a company name.

    For those of you following along at home: This gentlemen posted the same question in another forum where he referred to the compiler as "Siege Heil C" (his spelling).

Children
More questions in this forum