C7 · Lesson 20 · Shifters, multipliers and number formats
Moving bits, multiplying them, and representing fractions
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.
- A 2-bit × 2-bit multiplier: inputs and , four product bits .
- is one AND gate. adds two partial-product bits, and its carry flows into and , just as in long multiplication.
- Check a few rows of the truth table: 11 × 11 = 1001 (3 × 3 = 9).
Predict
The 8-bit two's-complement number −20 (11101100) is shifted right two places with an arithmetic shift. What results?
−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.
Name it
Shifters
A logical shift left by k multiplies by (bits fall off the top); a logical shift right divides an unsigned number by , filling with zeros. An arithmetic shift right fills with copies of the sign bit, dividing a two's-complement number by (rounding toward −∞). A barrel shifter shifts by any amount in one step with stages of 2:1 multiplexers, stage j shifting by or not.
Multipliers
An n × n multiply has n partial products, each shifted and ANDed with one bit of B; adding them gives a 2n-bit product. An array multiplier does it with AND gates and about 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 . Its resolution is , 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
| 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.
Worked example
Multiply 3 × 3 in the 2-bit multiplier, bit by bit.
Show the solution
- Bit 0.
- Bit 1. with a carry .
- Bit 2.
- Bit 3.
Answer the product is 1001 = 9.
Worked example
Encode 6.5 as an IEEE 754 single.
Show the solution
- Normalize.
- Sign. The number is positive, so the sign bit is 0.
- Exponent.
- Fraction. 101 followed by twenty zeros.
- Write it in hex. 0x40D00000.
Answer 0x40D00000.
Worked example
What number does the IEEE 754 single 0xC1480000 hold?
Show the solution
- Write it in binary. 1100 0001 0100 1000 0000 0000 0000 0000.
- Sign. The sign bit is 1: negative.
- Exponent. The next eight bits, 1000 0010 = 130, give the exponent
- Fraction. The fraction 100 1000 0... means = 1.5625.
- Put it together.
- The method. Reading a float is the encoding in reverse: split the fields, remove the bias, restore the hidden 1.
Answer −12.5.
Check
How many bits does the full product of two 8-bit numbers need?
Check
How many AND gates form the partial products of an 8 × 8 array multiplier?
Check
Shift the 8-bit two's-complement number −36 right by 1 places, arithmetically. Give the 8-bit result.
Check
How many multiplexer stages does a 32-bit barrel shifter need?
Check
A number in Q format with 2 fraction bits is stored as the integer 77. What value does it represent?
Check
What exponent field (in decimal) does an IEEE single store for the value , with p = 0?
Check
Write the product bit of a 2-bit × 2-bit multiplier.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
An IEEE 754 single has sign bit 0, biased exponent 120 and fraction 0.875.
(a)
What value does it hold?
(b)
What's the spacing between it and the next larger float?
Check
A floating-point format has m fraction bits and e exponent bits (bias ).
Step 1
What's the relative spacing of numbers just above 1?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What's the smallest normal number, ?
Explain why
Why does an arithmetic right shift divide a negative number correctly, while a logical shift doesn't?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.