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:
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
C. ~x+~y+1 == ~(x+y)
True
D. (ux-uy) ==-(unsigned)(y-x)
True
E. ((x >> 2) << 2) <= x
True
Last updated