CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: bitfield sizing

  1. #1
    Join Date
    Jun 2001
    Posts
    2

    bitfield sizing

    I wrote the code below to check the sizing of
    bitfield structures. In Visual C++, I get a size
    of 4 bytes no matter what I've tried. How do I
    get this to map to 2 bytes?

    Jim.
    #include <stdio.h>
    #include <windows.h>

    #pragma pack(1)

    // REGISTER WORD LAYOUTS
    // Host To IF Register
    typedef struct { // Mode
    unsigned reset_if : 1; // All
    unsigned service_request : 1; // RT
    unsigned subsystem_flag : 1; // RT
    unsigned host_to_micro_interrupt : 1; // BC
    unsigned dbcacc : 1; // RT
    unsigned start_minor_cycle : 1; // All
    unsigned sa30_wraparound : 1; // RT
    unsigned hbs_bor : 1; // All
    unsigned rt_address : 5; // All
    unsigned rt_parity : 1; // All
    unsigned enable_tx_error : 1; // BC
    unsigned interrupt_ctrl : 1; // All
    } HOST_TO_IF_REG_FIELDS;

    typedef union {
    HOST_TO_IF_REG_FIELDS field;
    USHORT value;
    } HOST_TO_IF_REG_TYPE;

    int main(int argc, char* argv[])
    {
    HOST_TO_IF_REG_TYPE content;

    content.value = 0xAAAA;
    printf("HOST to IF regtype size is %d\n", sizeof(content) );
    printf("reset_if: %08X\n", content.field.rt_address);
    return 0;
    }

    #pragma pack()


  2. #2
    Join Date
    Aug 1999
    Location
    Canada
    Posts
    2,076

    Re: bitfield sizing

    Try:


    // REGISTER WORD LAYOUTS
    // Host To IF Register
    typedef struct { // Mode
    unsigned short reset_if : 1; // All
    unsigned short service_request : 1; // RT
    unsigned short subsystem_flag : 1; // RT
    unsigned short host_to_micro_interrupt : 1; // BC
    unsigned short dbcacc : 1; // RT
    unsigned short start_minor_cycle : 1; // All
    unsigned short sa30_wraparound : 1; // RT
    unsigned short hbs_bor : 1; // All
    unsigned short rt_address : 5; // All
    unsigned short rt_parity : 1; // All
    unsigned short enable_tx_error : 1; // BC
    unsigned short interrupt_ctrl : 1; // All
    } HOST_TO_IF_REG_FIELDS;






  3. #3
    Join Date
    Jun 2001
    Posts
    2

    Re: bitfield sizing

    Worked great!


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