CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    27

    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!

  2. #2
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094
    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

  3. #3
    Join Date
    Nov 2003
    Posts
    27
    Is it legal usage in C like:
    unsigned int Cb2_superstate : 1;

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...

  6. #6
    Join Date
    Nov 2003
    Posts
    27
    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.

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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
  •  





Click Here to Expand Forum to Full Width

Featured