Name: Anonymous 2011-05-27 8:30
I am just beginning to learn C from K&R, and have a question regarding one of the examples in the book.
I quote:
'Longer intgers are converted to shorter ones or to chars by dropping the excess high-order bits. Thus in
int i;
char c;
i=c;
c=i;
the value of c is unchanged. this is true whether or not sign extension is involved. Reversing the order of assignments might lose information, however."
Now, assuming a machine where sign extension (if the MSB is 1, then, on promotion, it is carried over to the new MSB) is used and an initial value of 255 for c, c is promoted to a signed integer with a negative value, since its MSB is 1. i is given this value.
Now, when c is assigned i's value, i is converted to a single byte unsigned value. Since all bits beyond the 8th are dropped, so is the MSB, and i's new value is 127 - or 01111111 in binary. The MSB was lost in the transaction.
What am I missing here?
I quote:
'Longer intgers are converted to shorter ones or to chars by dropping the excess high-order bits. Thus in
int i;
char c;
i=c;
c=i;
the value of c is unchanged. this is true whether or not sign extension is involved. Reversing the order of assignments might lose information, however."
Now, assuming a machine where sign extension (if the MSB is 1, then, on promotion, it is carried over to the new MSB) is used and an initial value of 255 for c, c is promoted to a signed integer with a negative value, since its MSB is 1. i is given this value.
Now, when c is assigned i's value, i is converted to a single byte unsigned value. Since all bits beyond the 8th are dropped, so is the MSB, and i's new value is 127 - or 01111111 in binary. The MSB was lost in the transaction.
What am I missing here?