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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:45:12+00:00 2026-06-14T14:45:12+00:00

I have the following structure: struct { int a:4; int b:7; int c:21; }

  • 0

I have the following structure:

struct
{
   int a:4;
   int b:7;
   int c:21;
} example;

I would like to combine a and b to form an integer d in C++. For instance, I would like the bit values of a to be on the left of the bit values of b in order to form integer d. How is this implemented in c++?

Example:

a= 1001

b = 1010101

I would like int d = 10011010101 xxxxxxxxxxxxxxxxxxxxx

where x can be 21 bits that belonged to d previously. I would like the values of a and b to be put in bit positions 0-3 and 4-10 respectively since a occupies the first 4 bits and b occupies the next 7 bits in the struct “example”.

The part that I am confused about is that variable a and variable b both have a “sign” bit at the most significant bit. Does this affect the outcome? Are all bits in variable a and variable b used in the end result for integer d? Will integer d look like a concatenation of variable a’s bits and variable b’s bits?

Thanks

  • 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-14T14:45:14+00:00Added an answer on June 14, 2026 at 2:45 pm

    Note that whether an int bit-field is signed or unsigned is implementation-defined. The C++ standard says this, and the C standard achieves the same net result with different wording:

    ISO/IEC 14882:2011 — C++

    §7.1.6.2 Simple type specifiers

    ¶3 … [ Note: It is implementation-defined whether objects of char type and certain bit-fields (9.6) are
    represented as signed or unsigned quantities. The signed specifier forces char objects and bit-fields to be
    signed; it is redundant in other contexts. —end note ]

    §9.6 Bit-fields

    ¶3 … A bit-field shall have integral or enumeration type (3.9.1). It is
    implementation-defined whether a plain (neither explicitly signed nor unsigned) char, short, int, long,
    or long long bit-field is signed or unsigned.

    ISO/IEC 9899:2011 — C

    §6.7.2.1 Structure and union specifiers

    ¶10 A bit-field is interpreted as having a signed or unsigned integer type consisting of the specified number of bits.125)

    125) As specified in 6.7.2 above, if the actual type specifier used is int or a typedef-name defined as int, then it is implementation-defined whether the bit-field is signed or unsigned.

    §6.7.2 Type specifiers

    ¶5 … for bit-fields, it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.

    The context of §6.7.2 shows that int can be combined with short, long etc and the rule will apply; C++ specifies that a bit more clearly. The signedness of plain char is implementation-defined already, of course.


    Unsigned bit-fields

    If the type of the bit-fields are unsigned, then the expression is fairly straight-forward:

    int d = (example.a << 7) | example.b;
    

    Signed bit-fields

    If the values are signed, then you have a major interpretation exercise to undertake, deciding what the value should be if example.a is negative and example.b is positive, or vice versa. To some extent, the problem arises even if the values are both negative or both positive.

    Suppose example.a = 7; and example.b = 12; — what should be the value of d? Probably the same expression applies, but you could argue that it would be better to shift by 1 fewer places:

    assert(example.a >= 0 && example.b >= 0);
    int d = (example.a << 6) | example.b;      // Alternative interpretation
    

    The other cases are left for you to decide; it depends on the interpretation you want to place on the values. For example:

    int d = ((example.a & 0x0F) << 7) | (example.b & 0x7F);
    

    This forces the signed values to be treated as unsigned. It probably isn’t what you’re after.


    Modified question

    example.a = 1001     // binary
    
    example.b = 1010101  // binary
    
    d = 10011010101 xxxxxxxxxxxxxxxxxxxxx
    

    where x can be 21 bits that belonged to d previously.

    For this to work, then you need:

      d = (d & 0x001FFFFF) | ((((example.a & 0x0F) << 7) | (example.b & 0x7F)) << 21);
    

    You probably can use fewer parentheses; I’m not sure I’d risk doing so.

    Union

    However, with this revised specification, you might well be tempted to look at a union such as:

    union u
    {
        struct
        {
           int a:4;
           int b:7;
           int c:21;
        } y;
        int x;
    } example;
    

    However, the layout of the bits in the bit-fields w.r.t the bits in the int x; is not specified (they could be most significant bits first or least significant bits first), and there are always mutterings about ‘if you access a value in a union that wasn’t the last one assigned to you invoke undefined behaviour’. Thus you have multiple platform-defined aspects of the bit field to deal with. In fact, this sort of conundrum generally means that bit-fields are closely tied to one specific type of machine (CPU) and compiler and operating system. They are very, very non-portable at the level of detail you’re after.

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

Sidebar

Related Questions

gcc 4.4.4 c89 I have the following structure. struct device_sys { char device[STRING_SIZE]; int
I have program, that has structure defined like this: struct foo { int magic;
I have following structure struct { int myData; int myAnotherData; }Value; struct AnotherStructure {
I have the following structure: struct CountCarrier { int *CurrCount; }; And this is
I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I have the following structure: private struct S_indiv { public int[] x; public int
I'm using C++, and I have the following structures: struct ArrayOfThese { int a;
I have the following structure and function that adds things to the structure: struct
I am very beginner in C. I have the following structure typedef struct {
I have following structure with example data: id season_id title 1 1 Intro 2

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.