[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?

Re: Question about unions,bitfields

You may want to edit you post to properly format your code snippets:

- one line - one statement (no more than one statement!)
- 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!

Re: Question about unions,bitfields

Re: Question about unions,bitfields

Quote:

Originally Posted by

**2kaud**
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.

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"?

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].

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.

Re: Question about unions,bitfields