These properties are artifacts of the nite nature of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ive order of conversion from one data size to another and between unsigned and signed can affect the behavior of a program. Consider the following additional code for our previous example: 1 2 3 4 unsigned uy = x; /* Mystery! */ printf("uy = %u:\t", uy); show_bytes((byte_pointer) &uy, sizeof(unsigned)); 2.2. INTEGER REPRESENTATIONS This portion of the code causes the following to be printed: uy = 4294954951: ff ff cf c7 51 This shows that the expressions: (unsigned) (int) sx /* 4294954951 */ and (unsigned) (unsigned short) sx /* 53191 */ produce different values, even though the original and the final data types are the same. In the former expression, we first sign extend the 16-bit short to a 32-bit int, whereas zero extension is performed in the latter expression. 2.2.6 Truncating Numbers Suppose that rather than extending a value with extra bits, we reduce the number of bits representing a number. This occurs, for example, in the code: 1 2 3 int x = 53191; short sx = (short) x; int y = sx; /* -12345 */ /* -12345 */ On a typical 32-bit machine, when we cast x to be short, we tru...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online