The following is code which is run on a machine where values of type int are 32bits. They are represented in two's-complement, and right shifted arithmetically (meaning the bits are shift right, and the left bits are filled with the value of most significant bit). I'm assuming unsigned values are also 32 bits. Generating random values for x and y, I'm trying to find out whether the following expressions always yield a 1, or if there is a condition which makes the expression yield a 0.

int x = random();
int y = random();

/* convert to unsigned */
unsigned ux = (unsigned) x;
unsigned uy = (unsigned) y;

The following is what I have so far after testing with code:

A) (x < y) == (-x > -y)
False: If x= -2147483648 and y = 0, expression returns 0

B) ((x+y) <<4) + y - x == 17 * y + 15 * x
oddly enough I got False when x= 0 and y=0 ...not sure if that is correct nor the reasoning behind it though?

C) ~x + ~y == ~(x+y)
False for x=5 and y = 3

D) (int) (ux-uy) == - (y - x)
I think this is the only one I understand with some degree of certainty...it is true in all circumstances. Two's complement and unsigned addition/subtraction have the same bit level representations so ux-uy = x - y and distributing the right side we also get x - y

E) ((x >> 1) <<1) <= x
Seems to hold true for all values of x, because if you do a right arithmetic shift by one bit and then shift it back one bit then it ends up having the value x , or less than x if x originally contains all 1's.

Any corrections or help explaining any of the above would be greatly appreciated...my mind is spinning! Thanks

thanks

3. Please do not bump posts. If you have more specific questions about your clearly homework based questions, you may post them. Otherwise, you can wait and see if people will do your homework for you.

Further bumps will result in the thread being closed.

4. > oddly enough I got False when x= 0 and y=0 ...not sure if that is correct nor the reasoning behind it though
It's a fair bet that your test is wrong then.
Think <<4 and *16 and work from there.