+ 3

How this code is working? Anyone pls!

https://code.sololearn.com/ck4ivuD7jaQB/?ref=app

23rd Jan 2021, 7:42 AM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar
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.
23rd Jan 2021, 8:54 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
23rd Jan 2021, 8:23 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 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
23rd Jan 2021, 11:14 PM
Brian
Brian - avatar
+ 2
Thankyou
23rd Jan 2021, 8:56 AM
Nassera
Nassera - avatar
+ 1
Thanksgiving both.
23rd Jan 2021, 9:48 AM
<Saurabh Singh Rawat/>
<Saurabh Singh Rawat/> - avatar