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 731113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:59:21+00:00 2026-05-14T06:59:21+00:00

To begin with, the application in question is always going to be on the

  • 0

To begin with, the application in question is always going to be on the same processor, and the compiler is always gcc, so I’m not concerned about bitfields not being portable.

gcc lays out bitfields such that the first listed field corresponds to least significant bit of a byte. So the following structure, with a=0, b=1, c=1, d=1, you get a byte of value e0.

struct Bits {
  unsigned int a:5;
  unsigned int b:1;
  unsigned int c:1;
  unsigned int d:1;
} __attribute__((__packed__));

(Actually, this is C++, so I’m talking about g++.)

Now let’s say I’d like a to be a six bit integer.

Now, I can see why this won’t work, but I coded the following structure:

struct Bits2 {
  unsigned int a:6;
  unsigned int b:1;
  unsigned int c:1;
  unsigned int d:1;
} __attribute__((__packed__));

Setting b, c, and d to 1, and a to 0 results in the following two bytes:

c0 01

This isn’t what I wanted. I was hoping to see this:

e0 00

Is there any way to specify a structure that has three bits in the most significant bits of the first byte and six bits spanning the five least significant bits of the first byte and the most significant bit of the second?

Please be aware that I have no control over where these bits are supposed to be laid out: it’s a layout of bits that are defined by someone else’s interface.

  • 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-05-14T06:59:22+00:00Added an answer on May 14, 2026 at 6:59 am

    (Note that all of this is gcc-specific commentary – I’m well aware that the layout of bitfields is implementation-defined).

    Not on a little-endian machine: The problem is that on a little-endian machine, the most significant bit of the second byte isn’t considered “adjacent” to the least significant bits of the first byte.

    You can, however, combine the bitfields with the ntohs() function:

    union u_Bits2{
        struct Bits2 {
          uint16_t _padding:7;
          uint16_t a:6;
          uint16_t b:1;
          uint16_t c:1;
          uint16_t d:1;
        } bits __attribute__((__packed__));
        uint16_t word;
    }
    
    union u_Bits2 flags;
    flags.word = ntohs(flag_bytes_from_network);
    

    However, I strongly recommend you avoid bitfields and instead use shifting and masks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question more about how to build a c++ application than it
I have CLI/MFC application and I would like to begin to learn how to
I will begin this question by admitting I am very new to MVC. The
I have a question about improving the efficiency of my program. I have a
Q: This question is about how can i figure or select the convenient ORM
I've a small question I've a WPF application, and one method takes some times(it
I have a question about Entity Framework and Linq to Entities. My .NET version
I have a question about memory leaks in Qt. I have a QMainWindow with
I recently began profiling an osgi java application that I am writing using VisualVM.
Consider the following SQL: BEGIN TRAN SET TRANSACTION ISOLATION LEVEL READ COMMITTED INSERT Bands

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.