---
title: Problem set 4
heading: "Problem set 4: C, pointers, bits and integer arithmetic"
eyebrow: C8 · Problem set 4
type: problems
---

::: hook
These problems use lessons 17 to 21: how C compiles to assembly, pointers and memory-mapped registers, the routines of the assembly lab, multiplication, division and fixed point, and floating point. An `int` is 4 bytes, a `short` 2 and a `char` 1, and bits are numbered from 0 at the right.
:::

::: pool
pick: 12

::: check
kind: parts
given: b = 4096 | 8192 | 12288; i = 3 | 7 | 25

## An array starts at {b:hex4}, and the code reads element {i}.

::: part
answer: b + 4*i
base: 16
width: 4

### Where is `a[{i}]` if `a` is an `int` array?

::: misses
- [b + i] Scale the index by the element size, 4.
- [any] Not yet. base + 4i.
:::
:::

::: part
answer: b + 2*i
base: 16
width: 4

### Where is it if `a` is a `short` array?

::: misses
- [any] base + 2i.
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 8192; i = 2 | 5 | 10

## `struct point { int id; int x; int y; }` takes 12 bytes, with `id` at offset 0, `x` at 4 and `y` at 8. An array `pts` of these starts at {b:hex4}.

::: part
answer: b + 12*i + 8
base: 16
width: 4

### What address does `pts[{i}].y` read?

::: misses
- [b + 12*i] That's the start of the element; add the offset of y.
- [b + 4*i + 8] Each element is a whole struct, 12 bytes.
- [any] Not yet. base + 12i + 8.
:::
:::

::: part
answer: 8
tolerance: 0

### What offset does the `lw` use if a register already points at `pts[{i}]`?

::: misses
- [any] The field's offset within the struct.
:::
:::
:::

::: check
kind: parts
given: r = 0..255 int; k = 0 | 2 | 5 | 7; let bit = floor(r/2^k) - 2*floor(r/2^(k + 1))

## A device register holds {r:hex2}.

::: part
answer: r + 2^k*(1 - bit)
base: 16
width: 2

### What does it hold after `r |= (1 << {k})`?

::: misses
- [any] OR with the mask sets bit {k} and leaves the rest.
:::
:::

::: part
answer: bit
tolerance: 0

### What does `(r >> {k}) & 1` give for the original value?

::: misses
- [any] Bit {k} of the original.
:::
:::
:::

::: check
kind: parts
given: r = 0..255 int; k = 1 | 3 | 4 | 6; let bit = floor(r/2^k) - 2*floor(r/2^(k + 1))

## A device register holds {r:hex2}.

::: part
answer: r - 2^k*bit
base: 16
width: 2

### What does it hold after `r &= ~(1 << {k})`?

::: misses
- [any] AND with the inverted mask clears bit {k}.
:::
:::

::: part
answer: r + 2^k*(1 - 2*bit)
base: 16
width: 2

### What would it hold after `r ^= (1 << {k})` instead?

::: misses
- [any] XOR flips bit {k}.
:::
:::
:::

::: check
kind: parts
given: w = 2 | 3 | 4; p = 4 | 8 | 12

## A control register keeps a {w}-bit field in bits {p} upward.

::: part
answer: (2^w - 1)*2^p
base: 16
width: 4

### What mask selects the field?

::: misses
- [2^w - 1] Shift the mask up to bit {p}.
- [any] Not yet. $(2^w - 1) \cdot 2^p$.
:::
:::

::: part
answer: 2^w - 1
tolerance: 0

### What is the largest value the field can hold?

::: misses
- [any] $2^w - 1$.
:::
:::
:::

::: check
kind: parts
given: x = 0.2 | 0.35 | 0.6 | 0.9; y = 0.3 | 0.5 | 0.8

## Two numbers, {x} and {y}, are stored in Q15.

::: part
answer: round(x*32768)
tolerance: 0

### What integer holds {x}?

::: misses
- [any] Multiply by $2^{15}$ and round.
:::
:::

::: part
answer: floor((round(x*32768)*round(y*32768) + 16384)/32768)
tolerance: 0

### Their integers are multiplied, $2^{14}$ is added and the sum is shifted right by 15. What integer results?

::: misses
- [any] Not yet. $\lfloor (ab + 2^{14})/2^{15} \rfloor$.
:::
:::
:::

::: check
kind: parts
given: N = 10 | 12 | 16; V = 2500 | 3300 | 5000; f = 0.1 | 0.37 | 0.82; let c = floor(f*2^N)

## An {N}-bit ADC with a {V} mV reference reads {c}.

::: part
answer: floor(c*V/2^N)
unit: mV
tolerance: 0

### What does multiplying by {V} and shifting right by {N} give?

::: misses
- [any] $\lfloor cV/2^N \rfloor$.
:::
:::

::: part
answer: (2^N - 1)*V
tolerance: 0

### What is the largest product the multiply can produce?

::: misses
- [any] The largest code times the reference.
:::
:::
:::

::: check
kind: parts
given: a = 7 | 23 | 45; b = 2 | 4

## Signed division with `div` and `rem`, which round toward zero.

::: part
answer: -floor(a/b)
tolerance: 0

### What does `div` give for {a} ÷ −{b}?

::: misses
- [-floor(a/b) - 1] That rounds down; `div` rounds toward zero.
- [any] Not yet. Divide and drop the fraction toward zero.
:::
:::

::: part
answer: a - b*floor(a/b)
tolerance: 0

### What does `rem` give for {a} and −{b}?

::: misses
- [-(a - b*floor(a/b))] The remainder takes the sign of the dividend, which is positive here.
- [any] Not yet. dividend − divisor × quotient.
:::
:::
:::

::: check
kind: parts
given: n = 37 | 100 | 129 | 170 | 200; let s = floor(log(n + 0.5)/log(2)) + 1

## The assembly lab's bit counters are run on {n}.

::: part
answer: (n - 2*floor(n/2)) + (floor(n/2) - 2*floor(n/4)) + (floor(n/4) - 2*floor(n/8)) + (floor(n/8) - 2*floor(n/16)) + (floor(n/16) - 2*floor(n/32)) + (floor(n/32) - 2*floor(n/64)) + (floor(n/64) - 2*floor(n/128)) + floor(n/128)
tolerance: 0

### How many passes does the `x & (x - 1)` loop make?

::: misses
- [s] That's the loop that shifts right until x is 0. Clearing the lowest 1 takes one pass per 1 bit.
- [any] Not yet. One pass per 1 bit.
:::
:::

::: part
answer: s
tolerance: 0

### How many passes does a loop make that tests the low bit and shifts right until x is 0?

::: misses
- [any] One per bit up to the highest 1.
:::
:::
:::

::: check
kind: parts
given: c = 10 | 12 | 20 | 24 | 36 | 40 | 72; let p = floor(log(c + 0.5)/log(2))

## A compiler computes x × {c} as `(x << p) + (x << q)` with p > q.

::: part
answer: p
tolerance: 0

### What is p?

::: misses
- [any] The highest power of two in {c}.
:::
:::

::: part
answer: floor(log(c - 2^p + 0.5)/log(2))
tolerance: 0

### What is q?

::: misses
- [any] The power of two left over.
:::
:::
:::

::: check
kind: parts
given: b = 4096 | 8192; k = 3 | 8 | 20

## `int *p` and `char *q` both hold {b:hex4}.

::: part
answer: b + 4*k
base: 16
width: 4

### What address does `p + {k}` hold?

::: misses
- [b + k] Pointer arithmetic on an `int *` steps by 4 bytes.
- [any] Not yet. base + 4k.
:::
:::

::: part
answer: b + k
base: 16
width: 4

### What address does `q + {k}` hold?

::: misses
- [any] A `char *` steps by 1.
:::
:::
:::

::: check
kind: parts
given: x = 1.5 | 3.25 | -2.75 | 100.6

## A 16-bit Q8.8 number has 8 integer bits (sign included) and 8 fraction bits.

::: part
answer: round(x*256)
tolerance: 0

### What integer stores {x}?

::: misses
- [round(x*32768)] That's Q15. Q8.8 scales by $2^8$.
- [any] Not yet. Multiply by 256 and round.
:::
:::

::: part
answer: (2^15 - 1)/256
tolerance: 0.01%

### What is the largest value Q8.8 can hold?

::: misses
- [any] The largest 16-bit signed integer over 256.
:::
:::
:::

::: check
kind: parts
given: x = 25 | 777 | 4095; c = 3 | 5 | 9

## A compiler multiplies x = {x} by {c} with a shift and an add: (x << p) + x.

::: part
answer: round(log(c - 1)/log(2))
tolerance: 0

### What is p?

::: misses
- [any] $c - 1 = 2^p$.
:::
:::

::: part
answer: c*x
tolerance: 0

### What does it compute?

::: misses
- [any] $x(2^p + 1)$.
:::
:::
:::

::: check
kind: parts
given: a = 1.5 | -2.25 | 3.75; b = 0.5 | 1.25 | -0.75

## {a} and {b} are stored in Q8.8 and multiplied as integers.

::: part
answer: round(a*256)*round(b*256)
tolerance: 0

### What is the integer product?

::: misses
- [any] Multiply the two stored integers.
:::
:::

::: part
answer: floor(round(a*256)*round(b*256)/256)
tolerance: 0

### What Q8.8 integer does shifting right by 8 give?

::: misses
- [any] The product has 16 fraction bits; drop 8.
:::
:::
:::

::: check
kind: parts
given: n = 50 | 100 | 200 | 256 | 500 | 1000 | 1024

## `for (i = 0; i < n; i++) a[i] = b[i] + c;` runs with n = {n}.

::: part
answer: 8*n
tolerance: 0

### Indexed, each pass computes two addresses (`slli`, `add`, `add`), loads, adds, stores, increments i and branches: 8 instructions. How many in all?

::: misses
- [any] 8 per element.
:::
:::

::: part
answer: 6*n
tolerance: 0

### With two pointers walking (load, add, store, two pointer `addi`s, branch on an end pointer), how many?

::: misses
- [any] 6 per element.
:::
:::
:::

::: check
kind: parts
given: x = 5 | 300 | 12000 | 900000; N = 200 | 1000; c = 80 | 150; f = 64 | 200

## A filter on a microcontroller without an FPU works on floats near {x}, doing {N} software multiply-adds per sample at {c} cycles each, at {f} MHz.

::: part
answer: 2^(floor(log(x)/log(2)) - 23)
tolerance: 0.5%

### What's the spacing between floats near {x}?

::: misses
- [x*2^(-23)] Use the power of two at or below {x}.
- [any] Not yet. $2^{\lfloor\log_2 x\rfloor - 23}$.
:::
:::

::: part
answer: f*1e6/(N*c)
tolerance: 0.5%

### How many samples per second can it filter?

::: misses
- [f*1e6/N] Each multiply-add costs {c} cycles.
- [any] Not yet. $f/(Nc)$.
:::
:::

::: right
Right. Precision and speed both follow from the format and the hardware.
:::
:::
:::
