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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:06:03+00:00 2026-05-29T16:06:03+00:00

A typical use of bitfield is to declare a space efficient variable smaller than

  • 0

A typical use of bitfield is to declare a space efficient variable smaller than 8 bits. What i don’t understand is the value of declaring those bits as short, int , long , bool etc. For example

typedef struct{
    int first:3,
    short second:3,
    char third:3
    } somestruct;

In above case, all 3 variables, i.e. first, second and third are 3 bit long. What is the value of declaring the variable first as int, second as short and third as char?

Or, why is even a data type required? I should be able to declare the above as

typedef struct{
    first:3,
    second:3,
    third:3
    } modifiedstruct;

The modifiedstruct assumes no datatype for the variables first, second and third. The responsibility of interpreting the 3 bits as character, numeric or floating should be responsibility of something else.

Both gcc and g++ on linux allow the above behavior.

  • 1 1 Answer
  • 1 View
  • 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-29T16:06:03+00:00Added an answer on May 29, 2026 at 4:06 pm

    Actually, the C standard only allows bitfields to be of type signed int or unsigned int (and _Bool in C99). If you can throw a short, long or char in there, that’s a compiler extension.

    As to why, the main reason is signedness. Consider:

    struct {
       int s: 3;
       unsigned u: 3;
    } bf;
    
    bf.s = 7;
    bf.u = 7;
    

    Both of these bitfields are all ones. However, C preserves sign, so:

    (int)bf.s == -1    // Because signed conversions preserve the sign bit
    bf.s >> 1 == -1    // So do right shifts on signed values
    

    while:

    (int)bf.u == 7     // Because the source is unsigned and so just a series of bits
    bf.u >> 1 == 3     // Unsigned right shifts are just moving bits around as well
    

    For compilers that allow char, it’s probably the same sort of thinking. The default signedness of char is implementation-defined so if you want a bitfield’s signedness to match your compiler’s char’s signedness, you can define it as char.

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

Sidebar

Related Questions

My iPhone programs frequently contain grouped table views. A typical use might be for
Typical jQuery over-use: $('button').click(function() { alert('Button clicked: ' + $(this).attr('id')); }); Which can be
The use case I can think of is the following: In a typical application,
I am making a registration system with an e-mail verifier. Your typical use this
I'm familiar with the typical use of onload , as in the following: <body
The typical process we use is: Write code Checkin changes with a description of
A typical way to use JFileChooser includes checking whether user clicked OK, like in
Background My typical use case: # Table id category dataUID --------------------------- 0 A (NULL)
Can someone explain what a SpanQuery is, and what are typical use cases for
I am building a mobile web site for a physical product. The typical use-scenario

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.