---
title: Shifters, multipliers and number formats
heading: Moving bits, multiplying them, and representing fractions
eyebrow: C7 · Lesson 20 · Shifters, multipliers and number formats
xp: 40
---

::: hook
Adders are only the start of a processor's arithmetic. Shifting a binary number one place left doubles it, so shifters do fast multiplication and division by powers of two, and a barrel shifter moves bits any distance in one clock. Multiplication in binary is just shifted copies of a number added together, which an array of AND gates and adders does in hardware. And to handle fractions, numbers get a binary point: fixed in place for signal processing, or floating, with a separate exponent, in every scientific calculation.
:::

::: play
- A 2-bit × 2-bit multiplier: inputs $A_1A_0$ and $B_1B_0$, four product bits $P_3P_2P_1P_0$.
- $P_0$ is one AND gate. $P_1$ adds two partial-product bits, and its carry flows into $P_2$ and $P_3$, just as in long multiplication.
- Check a few rows of the truth table: 11 × 11 = 1001 (3 × 3 = 9).
:::
:::

```sim logic
input A1, A0, B1, B0
output P3, P2, P1, P0
assign P0 = A0 & B0
assign P1 = A1 & B0 ^ A0 & B1
assign c = A1 & B0 & A0 & B1
assign P2 = A1 & B1 ^ c
assign P3 = A1 & B1 & c
table: on
```

::: predict
## The 8-bit two's-complement number −20 (11101100) is shifted right two places with an arithmetic shift. What results?

- [ ] 59 (00111011)
- [x] −5 (11111011)
- [ ] −80 (10110000)
- [ ] 5 (00000101)

::: reveal
−5. An arithmetic right shift copies the sign bit into the vacated places, so a negative number stays negative and the shift divides by four: −20/4 = −5. A logical shift would fill with zeros and give 59, the unsigned meaning of the same bits.
:::
:::

::: nameit
## Shifters

A **logical** shift left by k multiplies by $2^k$ (bits fall off the top); a logical shift right divides an unsigned number by $2^k$, filling with zeros. An **arithmetic** shift right fills with copies of the sign bit, dividing a two's-complement number by $2^k$ (rounding toward −∞). A **barrel shifter** shifts by any amount in one step with $\log_2 n$ stages of 2:1 multiplexers, stage j shifting by $2^j$ or not.

## Multipliers

An n × n multiply has n **partial products**, each $A$ shifted and ANDed with one bit of B; adding them gives a 2n-bit product. An **array multiplier** does it with $n^2$ AND gates and about $n(n - 1)$ full adders, with a delay that grows as n. Faster designs reduce the partial products with trees of adders, and small processors multiply sequentially, one shift and add per clock.

## Fixed point

A **Qm.n** number is an ordinary integer with an implied binary point n bits from the right: its value is the integer over $2^n$. Its resolution is $2^{-n}$, and it adds with an ordinary adder. Multiplying two Qm.n numbers gives n extra fraction bits, which are shifted off.

## Floating point (IEEE 754)

A sign s, an exponent field e and a fraction f represent

$$(-1)^s \times 1.f \times 2^{e - \text{bias}}$$

| | sign | exponent | fraction | bias | significant digits |
|---|---|---|---|---|---|
| single (32 bits) | 1 | 8 | 23 | 127 | about 7 |
| double (64 bits) | 1 | 11 | 52 | 1023 | about 16 |

An exponent field of all 0s means zero or a tiny **subnormal** number; all 1s means infinity or NaN.
:::

::: example
## Multiply 3 × 3 in the 2-bit multiplier, bit by bit.

::: solution
1. **Bit 0.**
   $$\begin{aligned} P_0 &= A_0B_0 \\ &= 1 \end{aligned}$$
2. **Bit 1.**
   $$\begin{aligned} P_1 &= A_1B_0 \oplus A_0B_1 \\ &= 1 \oplus 1 \\ &= 0 \end{aligned}$$
   with a carry $c = 1$.
3. **Bit 2.**
   $$\begin{aligned} P_2 &= A_1B_1 \oplus c \\ &= 1 \oplus 1 \\ &= 0 \end{aligned}$$
4. **Bit 3.**
   $$\begin{aligned} P_3 &= A_1B_1c \\ &= 1 \end{aligned}$$

**Answer:** the product is 1001 = 9.
:::
:::

::: example
## Encode 6.5 as an IEEE 754 single.

::: solution
1. **Normalize.**
   $$\begin{aligned} 6.5 &= 110.1_2 \\ &= 1.101_2 \times 2^2 \end{aligned}$$
2. **Sign.** The number is positive, so the sign bit is 0.
3. **Exponent.**
   $$\begin{aligned} 2 + 127 &= 129 \\ &= 10000001_2 \end{aligned}$$
4. **Fraction.** 101 followed by twenty zeros.
5. **Write it in hex.** 0x40D00000.

**Answer:** 0x40D00000.
:::
:::

::: example
## What number does the IEEE 754 single 0xC1480000 hold?

::: solution
1. **Write it in binary.** 1100 0001 0100 1000 0000 0000 0000 0000.
2. **Sign.** The sign bit is 1: negative.
3. **Exponent.** The next eight bits, 1000 0010 = 130, give the exponent
   $$130 - 127 = 3$$
4. **Fraction.** The fraction 100 1000 0... means $1.1001_2$ = 1.5625.
5. **Put it together.**
   $$-1.5625 \times 2^3 = -12.5$$
6. **The method.** Reading a float is the encoding in reverse: split the fields, remove the bias, restore the hidden 1.

**Answer:** −12.5.
:::
:::

::: check
given: n = 4 | 8 | 16 | 32
answer: 2*n
tolerance: 0.1%

## How many bits does the full product of two {n}-bit numbers need?

::: right
Right. 2n = {answer}.
:::

::: misses
- [n] The largest product, $(2^n - 1)^2$, needs nearly 2n bits.
- [any] Not yet. Multiply the largest values.
:::
:::

::: check
given: n = 4 | 8 | 16
answer: n^2
tolerance: 0.1%

## How many AND gates form the partial products of an {n} × {n} array multiplier?

::: right
Right. One per bit pair: {answer}.
:::

::: misses
- [2*n] Every bit of A meets every bit of B.
- [any] Not yet. n partial products of n bits each.
:::
:::

::: check
given: x = -20 | -36 | -100; k = 1 | 2
answer: floor(x/2^k) + 256
base: 2
width: 8

## Shift the 8-bit two's-complement number {x} right by {k} places, arithmetically. Give the 8-bit result.

::: right
Right. The sign bit is copied in, so the value is $\lfloor x/2^k\rfloor$.
:::

::: misses
- [(x + 256)/2^k] That's a logical shift, filling with zeros: the value turns positive.
- [any] Not yet. Copy the sign bit into each vacated place.
:::
:::

::: check
given: n = 8 | 32 | 64
answer: log(n)/log(2)
tolerance: 0.1%

## How many multiplexer stages does an {n}-bit barrel shifter need?

::: right
Right. $\log_2 n$ = {answer}.
:::

::: misses
- [n] Each stage shifts by a power of two, so a few stages cover every distance.
- [any] Not yet. Stage j shifts by $2^j$ or not.
:::
:::

::: check
given: k = 3 | 77 | 200; n = 2 | 4
answer: k/2^n
tolerance: 0.1%

## A number in Q format with {n} fraction bits is stored as the integer {k}. What value does it represent?

::: right
Right. $k/2^n$ = {answer}.
:::

::: misses
- [k*2^n] The binary point divides the integer by $2^n$.
- [any] Not yet. The integer over $2^n$.
:::
:::

::: check
given: p = -10 | 0 | 2 | 20
answer: p + 127
tolerance: 0.1%

## What exponent field (in decimal) does an IEEE single store for the value $2^p$, with p = {p}?

::: right
Right. $p + 127$ = {answer}.
:::

::: misses
- [p] The exponent is stored with a bias of 127.
- [p + 128] The single-precision bias is 127.
- [any] Not yet. Add the bias.
:::
:::

::: check
kind: boolean
vars: A1, A0, B1, B0
answer: A1 & B0 ^ A0 & B1
ops: and, or, not, xor

## Write the product bit $P_1$ of a 2-bit × 2-bit multiplier.

::: right
Right. $A_1B_0 \oplus A_0B_1$: the sum of the two partial-product bits in that column.
:::

::: misses
- [A1 & B0 | A0 & B1] When both are 1, the column sums to 2: $P_1$ is 0 and a 1 carries.
- [any] Not yet. Add the two bits in the second column.
:::
:::

::: check
kind: parts
given: s = 0 | 1; e = 120 | 127 | 133; f = 0.25 | 0.5 | 0.875

## An IEEE 754 single has sign bit {s}, biased exponent {e} and fraction {f}.

::: part
answer: (1 - 2*s)*(1 + f)*2^(e - 127)
tolerance: 0.001%

### What value does it hold?

::: misses
- [(1 - 2*s)*f*2^(e - 127)] Restore the hidden 1.
- [(1 - 2*s)*(1 + f)*2^e] Subtract the bias, 127.
- [any] $(-1)^s(1 + f)2^{e - 127}$.
:::
:::

::: part
answer: 2^(e - 127)*2^(-23)
tolerance: 0.001%

### What's the spacing between it and the next larger float?

::: misses
- [any] One unit in the last place: $2^{e - 127} \times 2^{-23}$.
:::
:::
:::

::: check
kind: derivation
vars: m = 2..32, e = 2..15

## A floating-point format has m fraction bits and e exponent bits (bias $2^{e-1} - 1$).

::: step
answer: 2^(-m)

### What's the relative spacing of numbers just above 1?
:::

::: step
answer: 2^(1 - (2^(e - 1) - 1))

### What's the smallest normal number, $2^{1 - \text{bias}}$?
:::

::: right
Right. The fraction sets precision; the exponent sets range.
:::
:::

::: explain
## Why does an arithmetic right shift divide a negative number correctly, while a logical shift doesn't?

::: model
In two's complement the top bit carries a negative weight, and a negative number's high bits are all 1s. Shifting right moves every bit to half its weight; to keep the number's value halved, the new top bit must have the same meaning as the old one, so it must be a copy of the sign bit. A logical shift puts a 0 there instead, which turns the large negative weight into nothing and leaves a large positive number. Copying the sign bit keeps the pattern a valid two's-complement number with half the value, rounded down.
:::
:::

::: practice

::: check
given: x = 3 | 13 | 25; k = 1 | 3
answer: x*2^k
tolerance: 0.1%

## What is {x} shifted left by {k} places (no overflow)?

::: misses
- [any] Times $2^k$.
:::
:::

::: check
given: n = 4 | 8 | 12
answer: 2^(-n)
tolerance: 0.1%

## What is the resolution of a fixed-point format with {n} fraction bits?

::: misses
- [any] $2^{-n}$.
:::
:::

::: check
given: e = 120 | 127 | 130 | 140
answer: 2^(e - 127)
tolerance: 0.1%

## An IEEE single has sign 0, exponent field {e} and fraction 0. What value is it?

::: misses
- [any] $1.0 \times 2^{e - 127}$.
:::
:::
:::
