Integer Representations Exercises

Solutions

p1 (integer sizes)

How many bytes do the following types take up on a common 64-bit computer such as the Myth machines?

1. `unsigned long`
2. `short`
3. `signed char`

What C code could you write to determine the above?

p2 (integer ranges)

What is the range of integral values representable by each of the following types?

1. `unsigned long`
2. `short`
3. `signed char`

p3 (signedness)

Suppose Andrew writes `0b1111_1111` on the board. He assures you that this is a 8-bit integer. What value does it represent? (Hint: trick question)

p4 (two's complement)

1. You are given `0b1110_1011`, a `signed char`. Determine its decimal value.
2. You write the following code:

`signed char x = -13;`

What is the underlying bit pattern of the `signed char`?

p5 (overflow)

Alpha, Bravo, and Charlie are trying to write a C expression for the floored average of two `unsigned int`'s. (That is, the average rounded down to the nearest `unsigned int`.) What do you think about their proposals?

1. Alpha: `(x+y)/2`
2. Bravo: `x/2 + y/2`
3. Charlie: `x + (y-x)/2`