+ 3
How this code is working? Anyone pls!
5 Antworten
+ 6
TheGirlHasNoName in C there are no default values. This is called bit field and the numbers indicate the bits reserved for each field of the struct. That's also the reason why the code prints -123 instead of 123.
+ 3
I will expand on why it shows -128 versus 128. The highest bit of a signed int data type is interpreted as the sign bit (1 = negative). Because bit1 is only one bit wide, that bit *is* the highest bit. Therefore also it is the sign bit.
Here is a similar exercise using bit2. If you set the high bit in bit2 you will also see it turn negative. In line 9 try initializing bit2 to 9:
}bit={1,9,3};
Output: -1-73
Why -7?
9 = 1001 in binary
The high bit is 1. The negative interpretation is the negative two's complement (invert all bits and add 1).
two's complement: -(0110+1) = -0111 = -7
+ 2
Thankyou
+ 1
Thanksgiving both.