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

The Archive Base Latest Questions

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

Suppose I have struct A { signed char a:1; unsigned char b:1; }; If

  • 0

Suppose I have

struct A
{
    signed char a:1;
    unsigned char b:1;
};

If I have

A two, three;
two.a = 2; two.b = 2;
three.a = 3; three.b = 3;

two will contain 0s in its fields, while three will contain 1s. So, this makes me think, that assigning a number to a single-bit-field gets the least significant bit (2 is 10 in binary and 3 is 11).

So, my question is – is this correct and cross-platform? Or it depends on the machine, on the compiler, etc. Does the standard says anything about this, or it’s completely implementation defined?

Note: The same result may be achieved by assigning 0 and 1, instead of 2 and 3 respectively. I used 2 and 3 just for illustrating my question, I wouldn’t use it in a real-world situation

P.S. And, yes, I’m interesting in both – C and C++, please don’t tell me they are different languages, because I know 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-12T11:45:38+00:00Added an answer on June 12, 2026 at 11:45 am

    The rules in this case are no different than in case of full-width arithmetic. Bit-fields behave the same way as the corresponding full-size types, except that their width is limited by the value you specified in the bit-field declaration (6.7.2.1/9 in C99).

    Assigning an overflowing value to a signed bit-field leads to implementation-defined behavior, which means that behavior you observe with bit-field a is generally not portable.

    Assigning an overflowing value to an unsigned bit-field uses the rules of modulo arithmetic, meaning that the value is taken modulo 2^N, where N is the width of the bit-field. This means, for example, that assigning even numbers to your bit-field b will always produce value 0, while assigning odd numbers to such bit-field will always produce 1.

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

Sidebar

Related Questions

Suppose I have this struct (which incidentally contain bit-fields, but you shouldn't care): struct
Suppose I have this hypothetical, odd and unintuitive situation #include <iostream> struct A {
Let's suppose I have a struct like this: struct my_struct { int a; int
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
Suppose I have this code: struct Normal { public float x; public float y;
Suppose I have such a struct: struct Foo { const int bar; const char
Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose I have a C# struct like this: [StructLayout(LayoutKind.Explicit)] struct IMAGE_DOS_HEADER { [FieldOffset(60)] public
Suppose I have something like this: struct EmptyClass{}; template<typename T1, typename T2 = EmptyClass,
Suppose I have two struct s: struct X {}; struct Y { X x;

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.