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.
http://www.8052.com/forum/read.phtml?id=95746
How come you do not use "Siege Heil C" in this forum? you seem to have no problem with that in the other forum where you posted this question in. I quote queen Victoria "we are not amused. Erik
Try this:
typedef struct{ unsigned char a: 4; unsigned char b: 2; } my_type;
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).
The problem is that you've changed sizes of the integers into which the bit fields are supposed to be packed; the first is an int, and the second a char. This tells the compiler that you really do want two different integers, 2 bytes and 1 byte respectively. Try
typedef struct { unsigned int a: 4; unsigned int b: 2; } my_type;
This gentlemen posted the same question in another forum Gentleman??
Yep, that works. Thanks very much Mike. Regards, Murray R. Van Luyn.
Thanks Drew. That's answered a few questions. Regards, Murray R. Van Luyn.
The last time I saw bit fielding used on an 8051, the author eventually floundered the project and ultimately got fired. He couldn't understand why.
As it turns out, the poster is a college student in Australia, so there's probably little risk of him getting fired right at the moment. I also suppose that thinking back to myself in college, I'll cut him some slack on his distasteful joke.
My comment wasn't instigated from his description of Kiel on the other forum, rather it was an expression of my disdain for bit fields in C. There's many ways to torture and 8051, but few come close to the underlying assembly that results from bit fielding. I guess its OK if it simply has to be done in one or two spots, but anything more than that and its bad news.
"I guess its OK if it simply has to be done..." I can't think of an example where it would simply have to be done? Surely, in all cases, a shift and/or mask will be just as good?