-
April 22nd, 2004, 03:07 AM
#1
about boolean type
I have one piece of code:
typedef struct{
unsigned int Cb11_defaultstate2 : 1; /* boolean type */
unsigned int Cb10_basicstate2 : 1; /* boolean type */
unsigned int Cb6_basicstate1 : 1; /* boolean type */
unsigned int Cb7_basicstate3 : 1; /* boolean type */
unsigned int Cb8_defaultstate1 : 1; /* boolean type */
unsigned int Cb9_substate2 : 1; /* boolean type */
unsigned int Cb5_substate1 : 1; /* boolean type */
unsigned int Cb3_basicstate4 : 1; /* boolean type */
unsigned int Cb4_substate0 : 1; /* boolean type */
unsigned int Cb2_superstate : 1; /* boolean type */
} BFb7_tp;
... ...
I have not seen something like this, what does the ":1" mean? I guess it implies 1 bit. But will it use just one bit for every element?
As the code shows, the struct BFb7_tp should have the size of 10 bytes. But I use some tool to check the RAM size, and it needs just 2 bytes! That is, 1 byte for 8 elements! How can this happen?
Thank you in advance!
-
April 22nd, 2004, 03:29 AM
#2
It is 10 bits which is 2 bytes (6 bits unused). It will use 1 bit for every element I guess
Extreme situations require extreme measures
-
April 22nd, 2004, 03:41 AM
#3
Is it legal usage in C like:
unsigned int Cb2_superstate : 1;
-
April 22nd, 2004, 06:30 AM
#4
This is a so-called bit field and is a special case of a structure. In those structures the amount of bits for every component of it is separated with the : X.
1. Bit fields may be used inside a structure to pack several members into a single word of memory:
Code:
struct Foo
{
unsigned sevenbits : 7;
unsigned fourvals : 2;
} sFoo;
creates a structure with two members: 'sevenbits' which can hold values between 0 (binary: 0000000) and 127 (binary: 1111111), and 'fourvals' which can hold values between 0 (binary: 00) and 3 (binary: 11)
The bit field length may be a constant expression, similar to an array length specifier.
2. Bitfields are addressed just like other structure members:
Code:
sFoo.sevenbits = 32;
sFoo.fourvals /= 2;
3. Bitfields can be either signed or unsigned (though pre-ANSI compilers may only allow unsigned)
4. Bitfields may be unnamed, to act as padding between named fields:
Code:
struct Foo
{
unsigned sea : 3;
unsigned : 3;
unsigned see : 3;
};
5. The & operator cannot be applied to a bitfield, since a bitfield is only a subset of the bits in a word and thus has no address
6. Bitfields may be mixed in with other types in a structure
7. Some machine-level details of bitfields are implementation-defined:
- Bitfields may be stored left to right (big-endian) or right to left (little-endian), depending on the machine architecture
- Some compilers will allow bitfields to overlap a word boundary, others will silently add padding bits
- The maximum size of a bitfield is implementation-defined
8. These implementation-defined aspects make data created with bitfields inherently nonportable between dissimilar machines
-
April 22nd, 2004, 06:32 AM
#5
Re: about boolean type
Originally posted by wind0965
As the code shows, the struct BFb7_tp should have the size of 10 bytes. But I use some tool to check the RAM size, and it needs just 2 bytes! That is, 1 byte for 8 elements! How can this happen?
Well...the size is 4 bytes due to the underlying datatype used (-> unsigned int) which is 4 bytes on a windows system...
-
April 22nd, 2004, 09:51 AM
#6
Well, you are right. But I am sorry to forget to mention that it is a programm for 16 bits microcontroller.
Anyway, thank you very much.
-
April 22nd, 2004, 04:17 PM
#7
Originally posted by wind0965
Well, you are right. But I am sorry to forget to mention that it is a programm for 16 bits microcontroller.
Anyway, thank you very much.
Well...okay...in this case the size of an integer would rather be 16-bit...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|