Keil Logo

constant bit-fields in struct

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Rock nMoon
Posted
3-Jun-2002 08:23 GMT
Toolset
C51
New! constant bit-fields in struct
Hi All,,

How can i declare constant bit fields in a struct ??

struct mystruct{
unsigned char xx:6;
unsigned char yy:2;
}mystruct;

Let us say i want mystruct.yy to be a constant 00, how can i do that ??

mystruct must be a 8-bit datatype. which of its two components xx and yy would be at MSB ?? If anyone can tell how to figure that out it would be great.

Thankzz && Bye
-Rocknmoon
Read-Only
Author
Mike Kleshov
Posted
3-Jun-2002 12:47 GMT
Toolset
C51
New! RE: constant bit-fields in struct
You declare constant structure members the same way you declare constant variables:
struct mystruct {
   unsigned char xx:6;
   unsigned char const yy:2;
} mystruct = { 3, 0 };
Note that you have to initialize mystruct because later the compiler will not let you modify the constant yy field. Of course you can cheat and use typecast to modify yy:
(unsigned char)mystuct.yy = 1;
But if you do that, what's the point in having a constant struct member?
Which of the structure members are MSB or LSB you can find out by testing the code. Bit-fields are implementation specific. Here is one of possible tests:
mystruct = { 0, 1 };
printf("LSB=%d", (1 & *(char*)&mystruct) + '0');
- Mike
Read-Only
Author
Rock nMoon
Posted
3-Jun-2002 13:59 GMT
Toolset
C51
New! RE: constant bit-fields in struct
struct mystruct {
unsigned char xx:6;
unsigned char const yy:2;
} mystruct = { 3, 0 };

Would this struct take up 1 byte of storage space or 2 bytes on a low end 8051 architecture ??
Read-Only
Author
Mike Kleshov
Posted
3-Jun-2002 15:03 GMT
Toolset
C51
New! RE: constant bit-fields in struct
Why ask if you can check yourself? I do C166, so I can't help you with that.
Write a small program, run it in simulator, check sizeof(mystruct). It will take you 3 minutes.
In the declaration of struct member the const qualifier doesn't mean it goes into ROM. A variable cannot be partly in ROM and partly in RAM.
Regards,
- Mike
Read-Only
Author
Andrew Neil
Posted
4-Jun-2002 10:26 GMT
Toolset
C51
New! RE: constant bit-fields in struct
"Which of the structure members are MSB or LSB you can find out by testing the code."

Or you can read the manual

You might also want to check these articles:
http://www.keil.com/support/docs/928.htm
http://www.keil.com/support/docs/1279.htm

And this thread:
http://www.keil.com/forum/docs/thread1530.asp

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.