CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2009
    Posts
    40

    [RESOLVED] Question about unions,bitfields

    Code:
     #include <iostream>
    
    using namespace std;
    
    struct dore
    {
    
    
       unsigned b1:1;
       unsigned b2:1;
       unsigned b3:1;
       unsigned b4:1;
       unsigned b5:1;
       unsigned b6:1;
       unsigned b7:1;
       unsigned b8:1;
       unsigned b9:1;
       unsigned b10:1;
       unsigned b11:1;
       unsigned b12:1;
       unsigned b13:1;
       unsigned b14:1;
       unsigned b15:1;
       unsigned b16:1;
       unsigned b17:1;
       unsigned b18:1;
       unsigned b19:1;
       unsigned b20:1;
       unsigned b21:1;
       unsigned b22:1;
       unsigned b23:1;
       unsigned b24:1;
       unsigned b24:1;
       unsigned b25:1;
       unsigned:b26:1;
       unsigned b27:1;
       unsigned b28:1;
       unsigned b29:1;
       unsigned b30:1;
       unsigned b31:1;
       unsigned b32:1;
    };
    
    
    union s
    {
       int num;
       struct dore ya2;
      
    };
    
    
    int main()
    {
        int sayi;
        s yapi1;
       
        cout << "Enter a number";
        cin >> sayi;
        yapi1.num=sayi;
        
        cout << yapi1.num << endl;
        cout << sizeof(yapi1.num);
    
         if (yapi1.ya2.b1==1) cout << "1";
         else cout << "0";
         if (yapi1.ya2.b2==1) cout << "1";
         else cout << "0";
       
    
          if (yapi1.ya2.b3==1) cout << "1";
         else cout << "0";
         if (yapi1.ya2.b4==1) cout << "1";
         else cout << "0";
    
         if (yapi1.ya2.b5==1) cout << "1";
         else cout <<"0";
         if (yapi1.ya2.b6==1) cout << "1";
         else cout <<"0";
    
         if (yapi1.ya2.b7==1) cout << "1";
         else cout <<"0";
         if (yapi1.ya2.b8==1) cout << "1";
         else cout <<"0";
    
    
    }
    This code works,it prints the first 8 bits of the given number.

    But if I change this code:
    Code:
     unsigned b1:1;
    unsigned b2:1;
    unsigned b3:1;
    unsigned b4:1;
    unsigned b5:1;
    unsigned b6:1;
    unsigned b7:1;
    unsigned b8:1;
    unsigned b9:1;
    unsigned b10:1;
    ...
    };
    to :
    Code:
    int b1:1;
    int b2:1;
    int b3:1; 
    ...
    then it doesn't work right.Why does using unsigned work although using int doesn't work as I expected?
    Last edited by AwArEnEsS; April 22nd, 2013 at 11:03 PM. Reason: I reformatted code snippets

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Question about unions,bitfields

    You may want to edit you post to properly format your code snippets:
    1. one line - one statement (no more than one statement!)
    2. the same - for the declaration, so rather than put it in one line with comma delimiter like
      Code:
      unsigned b1:1,b2:1,b3:1,b4:1,b5...
      please, put one and only one declaration in a line
      Code:
      unsigned b1:1;
      unsigned b2:1;
      unsigned b3:1;
      unsigned b4:1;
      ...

    Otherwise your code is absolutely unreadable!
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Question about unions,bitfields

    int is a signed number.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Question about unions,bitfields

    Quote Originally Posted by 2kaud View Post
    int is a signed number.
    Yeah, I agree to that being the probable reason for the failure. I didn't really test/investigate this, but I think a one-bit signed integer made up by such a bit field quasi comprises a sign bit only. If that bit is set and that gets sign-extended to one of the common signed integer types with more bits, the result will probably be -1. So I think the OP's signed int code would work if it were changed to comparison against -1 rather than 1.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Oct 2009
    Posts
    40

    Re: Question about unions,bitfields

    Thanks for your answers.Yes you are right,I tested and those int bitfield variables take "-1".

    But I have problems understanding why they are "-1" and not "1"?Yes int is a signed type but signed int type includes "1" too I think,why does it take "-1" instead of "1"?
    Last edited by AwArEnEsS; April 23rd, 2013 at 08:52 AM.

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Question about unions,bitfields

    first of all, this is all implementation defined behavior. Even the signedness of an "int" bitfield is implementation defined ( you should write "signed int" to enforce a signed integer, although int and signed int are always the same type ... don't ask me why ). It's also implementation defined how they are packed/aligned or if they are stored from left to right or vice versa, etc ...

    regarding your observed behavior, in two-complement a signed bitfield of n-bits gives a range of values [-2^(n-1),+2^(n-1)-1]; for a 1-bit-field this means a range [-1,0].

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Question about unions,bitfields

    1) the standard defines bitfields only for unsigned int, int and signed int. other types may or may not be supported by your compiler
    2) the standard only defines the behaviour of bitfields.
    3) the standard does not define actual implementation of how the bits are packed into the large type. hence bitfields are compiler dependant and are not binary portable.
    If your application requires specific implementation of sequences of bits stored into a larger type, you need to shift/mask yourself, bitfields are NOT a portable solution for this.
    4) there is no such thing as a "sign bit" in C++ or at least not one that is guaranteed. most computers store negative numbers as 2's complement some store it as one's complement. Because of this. bitfields of 1 bit in a signed type make little sense and even behaviour isn't guaranteed to be portable in this case.
    On a One complement machine a 1 bit signed bitfield will hold either 0 or 1, on a Two's complement machine it will hold -1 or 0.

  8. #8
    Join Date
    Oct 2009
    Posts
    40

    Re: Question about unions,bitfields

    Thanks for your answers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured