- Given: int a = 135; When examining the four consecutive bytes of memory in inc
ID: 3591067 • Letter: #
Question
- Given: int a = 135;
When examining the four consecutive bytes of memory in increasing order starting at &a,
we find the following: 10000111 00000000 00000000 00000000
A. This is likely to be a little endian machine.
B. If reading only the first byte, this could be a negative 1-byte integer.
C. The consecutive bytes could represent a floating point number.
D. Both A and C above are correct.
E. All of the above are correct.
- When casting the int a from part (a) above to a char (which is a single byte), what value will we get?
A. 135
B. 0
C. a negative value
D. undefined value
E. None of these
I will really appreciate if you can explain why.
Explanation / Answer
1)
In little endian the least significant byte will be placed first followed
by the other bytes up to Most significant byte.
If we take the given data 10000111 00000000 00000000 00000000
Here 10000111 is the LSB and 00000000 is the MSB.
Hence the actual value is 00000000 00000000 00000000 10000111
The value in decimal is 135.
Hence option A is true this is in little endian.
The 1st byte here is 10000111 whose value is -121.
Here Most significant bit is 1 which indicates it is -ve value.
and the value in 2's complement is -128 + 7 = -121 [-sign bit value(128) + data value(111 = 7)]
Hence option B is true we will get -ve value if we only read the 1st byte.
This can represent a floating point number as there are 32 bits
we can take 8 bits for exponent 1 bit for sign and 23 bits for mantisa.
Hence option C is true the consecutive bytes could represent a floating point number.
Hence all the above are True.
2)
Here if we cast int a; with char then this char variable
will point only 1 byte of that integer as we know char
can handle only 1 byte.
Hence it can only handle 10000111 and this value is -121.
Hence option C. a negative value is correct.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.