| Next | Making Programs Faster | 121 |
Let me throw around my math skills... Recalling some binary
math I figured that 10^2 (10 to the power of 2) may be
simplified into this: 10^2 = 10x10 = 10x(2x2x2+2).
Notice all the 2's there? Here's where the left shift
operator '<<' comes in handy (and it's pretty fast by the
way).
So, every multiplication by 2 could be replaced by a left
shift by one (in binary it's equivalent to multiplying by 2
;) like this:
10^2 = 10<<3 + 10<<1; (by the way, this is may not be written
as 10<<4! :)
So, I've replaced 10x10 by a few left shift operators. The
key here is to determine how many left shifts will have to be
performed for given power.
Etc.
Now, if we were programming in assembly language, maybe
(Maybe not)
| Next | ![]() |
Copyright © 2003 M. J. Dominus |