Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:43:26+00:00 2026-06-10T05:43:26+00:00

I have to program independent bits in a bitfield spanning multiple DWORDS. I am

  • 0

I have to program independent bits in a bitfield spanning multiple DWORDS. I am currently using a struct as follows

typedef struct _myStruct
{
    union 
    {
        struct 
        {
            DWORD   field1       : 16
            DWORD   field2       : 8
            DWORD   field3       : 8
        };
        DWORD   value0;
    };
    union 
    {
        struct 
        {
            DWORD   field4       : 32; 
        } 
        DWORD   value1;
    };
} myStruct;

I do this so that a programmer can access independent fields directly, and not remember the corresponding DWORD i.e. as myStruct.field1 etc.
This works well in Visual Studio, however GCC complains when I used unnamed structs and unions. To correct that I tried naming the unions and structs as below:

    union _DW0
    {
        struct _BF
        {
            DWORD   field1       : 16
            DWORD   field2       : 8
            DWORD   field3       : 8
        } BF;
        DWORD   value0;
    } DW0;

But now the access is not programmer friendly.. i.e. someone who tries to program this have to remember which DWORD each field belongs to. For eg: myStruct.DW0.field1

Is there a way to get around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:43:27+00:00Added an answer on June 10, 2026 at 5:43 am

    Bitfields are inherently non-portable. When you write DWORD field1 : 16; the standard does not determine whether field1 should have the higher or lower 16 bits of the struct. On the other hand, if you use proper types and unions (which in your case suffice, since all your bitfields match a type in most platforms), that can be portable.

    Using C++11 types (you can alternatively use a library that has the proper types for your platform):

    union {
       struct {
          uint16_t  _1;
          uint8_t   _2;
          uint8_t   _3;
       } field;
       uint32_t value;
    };
    // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a C++ program where I have vectors of independent and dependent
I have an update program that is completely independent of my main application. I
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I have a program that gets a JSON from the server using getJSON and
I have a program with a MenuStrip at the top, it currently has 4
I have a python program that performs several independent and time consuming processes. The
I have an assignment in a language-independent class, and part of it is using
I currently have an assembly program which is modeled after the hexdump system function
I have program written in C. It takes 2 arguments username/password and try to
I have program that requires Python 3, but I develop Django and it uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.