> For the complete documentation index, see [llms.txt](https://valineliu.gitbook.io/deuterium-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://valineliu.gitbook.io/deuterium-wiki/reading/cs-jing-dian-shu-ji/csapp-3e-homework-solution/2.-representing-and-manipulating-information/2.82-signed-and-unsigned.md).

# 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:

```c
/* 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

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

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

True

```c
~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
