struct A
{
int a:2;
int b:3;
int c:3;
};
int main()
{
struct A p = {2,6,1};
printf("\n%d\n%d\n%d\n",p.a,p.b,p.c);
return 0;
}
Output is: -2,-2,1
What would be output of above code in C complier and in C++ complier? And Why?
Your system seems to using 2's complement. A 2-bit
bit-field holding 2
would be 10
in binary which is -2
in 2's complement system. Likewise 110(6)
is -2
for a 3-bit
representation in 2's complement. And 1
is plain 1
Also read about signed bit-fields here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With