This preview shows page 1. Sign up to view the full content.
Unformatted text preview: s, while retaining the same numeric value. Of course, this may not be possible when the destination data type is too small to represent the desired value. Converting from a smaller to a larger data type, however, should always be possible. To convert an unsigned number to a larger data type, we can simply add leading 0s to the representation. this operation is known as zero extension. For converting a two’s complement number to a larger data type, the rule is to perform a sign extension, adding copies of the most signiﬁcant bit to the representation. Thus, if our original value has bit representation ÜÛ ½ ÜÛ ¾ Ü¼ , the expanded representation would be ÜÛ ½ ÜÛ ½ ÜÛ ½ ÜÛ ¾ Ü¼ . As an example, consider the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 short sx = val; /* -12345 */ unsigned short usx = sx; /* 53191 */ int x = sx; /* -12345 */ unsigned ux = usx; /* 53191 */ printf("sx = %d:\t", sx); show_bytes((byte_pointer) &sx, sizeof(short)); printf("usx = %u:\t", usx); show_bytes((byte_pointer) &usx, sizeof(unsigned short)); printf("x =...
View Full Document