Question 1 (Easy)

Represent the following numbers in binary (assume $16$-bit length words):

• $-122_{}$ to base-$2$.
• $-7_{}$ to base-$2$.
• $22_{}$ to base-$2$.

Solutions

• $-122_{}$ is the same as $1111111110000110_{}$
• $-7_{}$ is the same as $1111111111111001_{}$
• $22_{}$ is the same as $0000000000010110_{}$

Question 2 (Hard)

You are given this mystery function.

int spooky(int a, int b)
{
if (b == 0) return a;
int x = a ^ b;
int c = (a & b) << 1;
return mystery(x, c);
}


What does it do?

Question 3 (Medium)

Determine which, if any, of the following operations are equal for all values. If they are not always equal, provide a counterexample.

1. $\frac{x}{2} - \frac{y}{2}$ $=$ $\frac{x - y}{2}$
2. $(x - y)\times z$ $=$ $(x \times z) - (y \times z)$
3. $(x - y) + z$ $==$ $x + (z - y)$

Solution

1. No. Consider where $x = \texttt{int_max}$ and $y = -10$.
2. The operation holds for all values.
3. The operation also holds for all values.

Question 4 (Easy)

Find the result of the following bitwise operations.

1. 0xFF00800F | 0x00FFF000
2. 0x00F00FFF ^ 0x00000000
3. 0x800F0FFF >> 4
4. (0xFF00FF0 << 8) & 0x000FF0F0

####Solution

1. 0xFFFFF00F
2. 0x00F00FFF
3. 0XF800F0FF
4. 0X000F0000

####