Multiplication, Shifts and Division by Powers of Two
Multiplying in binary is grade-school long multiplication where every row is either 0 or the top number shifted left, so it's only shifts and adds. Shifting left by multiplies by and shifting right divides by , but for negative numbers the arithmetic right shift rounds down toward , not toward zero the way division in C does.
What the symbols mean
- Bit i of B
Decides whether this row is A shifted, or zero.
- A shifted left i places
Equal to A · 2ⁱ.
What the symbols mean
- Shift amount
How many places the bits move. The brackets ⌊ ⌋ mean round down, toward −∞: ⌊−3.5⌋ = −4.
Explanation
Binary multiplication is simply long multiplication from grade school, done with digits that are only 0 or 1. That makes it easy: each row of the long multiplication, a partial product, is either all zeros or a copy of the top number shifted over. The formula says exactly that: for each bit of that's 1, take shifted left places (, which is ), and add them all up. Shifts themselves are covered in Bitwise Operations; here we look at what they cost in hardware and exactly how they round. It's just adding shifted copies; bear with it.
Shift-and-add. Take . In binary , so bits 0, 1 and 3 are set, and
Why it works: , and multiplication distributes over addition, so . A simple hardware multiplier is a loop of steps: look at the next bit of , and if it's 1, add the shifted into a running total. That's additions, each through an -bit adder.
How wide, and how many rows. An -bit multiply has partial products, and the product needs up to bits, since . Instruction sets split it: RISC-V's mul returns the low bits and mulh/mulhu the high . The low half is the product mod , the same for signed and unsigned (the wheel again), but the high half depends on the reading, so it needs two instructions. Fast multipliers don't loop: they form all partial-product bits at once with AND gates and add the rows in a tree of adders, so the depth grows like instead of .
Fewer 1 bits in means fewer rows, which is why compilers turn x * 10 into (x << 3) + (x << 1). Subtraction helps with runs of 1s: , so x * 15 is (x << 4) - x, one operation instead of three additions.
Left shift multiplies, mod . Shifting left by moves every bit to a place worth times as much, and bits pushed off the top are lost, so : ordinary overflow.
Right shift: logical or arithmetic. Shifting right drops the low bits. A logical shift fills the top with 0s, which divides an unsigned number by , rounding down. An arithmetic shift fills the top with copies of the sign bit, and for a signed number it gives exactly . Here's why. Write with , so and is just the low bits. Dropping those bits leaves the bits of , and filling the top with the sign bit is sign extension, which keeps 's value. So the result is . Once again, the bits don't decide: shr reads the register as unsigned, sar as signed.
Floor vs truncation. For negatives, rounds toward : . But division in C, Java and most languages truncates toward zero: -7 / 2 is . So a compiler can't turn x / 2 into x >> 1 for a signed x on its own. The fix is the last formula: for negative , add before shifting, which turns rounding down into rounding up (toward zero, for negatives). For example . The formula holds because adding pushes past the next multiple of unless was already sitting on one. There's an animation further down that plots the two roundings side by side.
A common mistake. It's tempting to think x >> 1 is the same as x / 2 in code. For unsigned numbers, and signed numbers that aren't negative, it is. For negative signed numbers the shift rounds down and the division rounds toward zero, so is but is . Compilers add a small correction before the shift; if you hand-optimize divisions into shifts, you have to as well.
See it
Worked examples
Multiply () by shift-and-add.
- 1
List the bits of the multiplier from the right: , , , .
Compute (an arithmetic shift) on 8-bit values, compare it with in C, and apply the compiler's fix.
- 1
Write in 8 bits: ; flip to ; add 1 to get .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Using shift-and-add, which shifted copies of get added to compute ?
An 8-bit unsigned register holds . What does it hold after shifting left by 3?
Compute (arithmetic shift) and in C. Enter both, shift first.
The byte is shifted right by 3, once with a logical shift (reading it as unsigned ) and once with an arithmetic shift (reading it as signed). Give both results as numbers, logical first.
A digital audio filter scales a signed sample by with (x >> 1) + (x >> 2), using arithmetic shifts. What does it output for ?
An array multiplier for two 32-bit numbers forms every partial-product bit with its own AND gate. How many AND gates does it use, and how many bits wide is the full product? Enter both.
Plain shift-and-add computes by adding four shifted copies of (since ). Writing gives (x << 4) - x instead. How many add or subtract operations does each way take? Enter the way first.
The hardest one here. Get it, and you've got this lesson.
A compiler divides a signed 32-bit by 16, rounding toward zero, with (x + ((x >> 31) & 15)) >> 4, using arithmetic shifts. For , what is the bias (x >> 31) & 15, what is the final result, and what would a plain x >> 4 have given? Enter all three.
Where it's used
Compilers replace multiplication and division by constants with shifts, adds and the sign-bias trick, because a shift takes one cycle and a hardware divide can take dozens. Fixed-point signal processing on small chips is built from multiplies followed by arithmetic right shifts, and its designers track the round-down bias of every shift.