= Binary Math =
= Number Types =
= Multiplication and Division =
Never, ever do multiplication or division. Ok, you can sometimes, but you need to be careful about it because it is extremely slow. A multiply takes '''10 cycles'''! Compare this to an INCA that takes 2 cycles.
Now look at a division. '''41 cycles'''! Yikes. This is forever in computer terms.
Fortunately there is a better way: shifting.
It is very easy to mutliply or divide by factors of 2 in binary.
Look at the number 3 in binary: %00000011. Now look at the number 3*2 = 6: %00000110.
Notice a pattern? You simply shift the bits left by one to multiply by 2.
To multiply by 4 you shift the bits left by 2 positions. Ie 3*4 = 12: %00001100, or $000C.
Dividing is the same way, but shifting right.
In assembly terms, these are the logical shift instructions: LSLA, LSLB, LSL, LSRA, LSRB, LSR.
There is a
summary of shifts that is very informative, and explains the difference between arithmetic and logical shifts.
--
AlexHarford - 30 Jun 2006
Topic revision: r1 - 30 Jun 2006 - 23:01:27 -
AlexHarford