2.82 Signed and Unsigned

Problem:

We are running programs on a machine where values of type int are 32 bits. They are represented in two's complement, and they are right shifted arithmetically. Values of type unsigned are also 32 bits.

We generate arbitrary values x and y, and convert them to unsigned values as follows:

/* Create some arbitrary values */
int x = random();
int y = random();
/* Convert to unsigned */
unsigned ux = (unsigned) x;
unsigned uy = (unsigned) y;

For each of the following C expressions, you are to indicate whether or not the expression always yields 1. If it always yields 1, describe the underlying mathematical principles. Otherwise, give an example of arguments that make it yield 0.

A. (x<y) == (-x>-y)

False, x=TMin

B. ((x+y)<<4) + y-x == 17*y +15*x

True

((x+y) << 4) + y-x
= 16*x + 16*y +y -x
= 17*7 + 15*x

C. ~x+~y+1 == ~(x+y)

True

~x+~y+1
= ~x+1 + ~y+1 -1
=-x + -y - 1
= -(x+y) - 1
= ~(x+y)

D. (ux-uy) ==-(unsigned)(y-x)

True

E. ((x >> 2) << 2) <= x

True

Last updated