C720 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 A1A0 and B1B0, four product bits P3P2P1P0.
  • P0 is one AND gate. P1 adds two partial-product bits, and its carry flows into P2 and P3, 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?

Name it

Shifters

A logical shift left by k multiplies by 2k (bits fall off the top); a logical shift right divides an unsigned number by 2k, filling with zeros. An arithmetic shift right fills with copies of the sign bit, dividing a two's-complement number by 2k (rounding toward −∞). A barrel shifter shifts by any amount in one step with log2⁡n stages of 2:1 multiplexers, stage j shifting by 2j 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 n2 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 2n. 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×1.f×2e−bias
signexponentfractionbiassignificant digits
single (32 bits)1823127about 7
double (64 bits)111521023about 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
  1. Bit 0. P0=A0B0=1
  2. Bit 1. P1=A1B0⊕A0B1=1⊕1=0 with a carry c=1.
  3. Bit 2. P2=A1B1⊕c=1⊕1=0
  4. Bit 3. P3=A1B1c=1

Answer the product is 1001 = 9.

Worked example

Encode 6.5 as an IEEE 754 single.

Show the solution
  1. Normalize. 6.5=110.12=1.1012×22
  2. Sign. The number is positive, so the sign bit is 0.
  3. Exponent. 2+127=129=100000012
  4. Fraction. 101 followed by twenty zeros.
  5. Write it in hex. 0x40D00000.

Answer 0x40D00000.

Worked example

What number does the IEEE 754 single 0xC1480000 hold?

Show the 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.10012 = 1.5625.
  5. Put it together. −1.5625×23=−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

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 2p, with p = 0?

Check

Write the product bit P1 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 2e−1−1).

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°.

Explain why

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

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.