#28330 - isildur - Fri Oct 29, 2004 7:50 pm
I know you can avoid multiplication using shifts and adds like this:
Code: |
// 45 * 100 = 4500;
(45 << 6) + (45 << 5) + (45<<2) = 4500;
|
That said, is it possible to perform division using a similar method?
#28334 - yaustar - Fri Oct 29, 2004 8:22 pm
yes by using bitshift right ( >> ) but te same limitations apply (only divisions of 2, 4 ,8, etc) and and rounds down to the nearest integer:
eg 9 >> 2 = 2
_________________
[Blog] [Portfolio]
#28335 - isildur - Fri Oct 29, 2004 8:25 pm
I know that, ;-).
I am not talking about divisions by powers of 2. In my example above, I avoid a multiply by adding the result of several shifts. I meant dividing any numbers by using successive shifts added and/or substracted to obtain the result, of course losing the decimal part.
#28337 - poslundc - Fri Oct 29, 2004 8:50 pm
If you know the binary representation of the reciprocal then you should be able to formulate it through a series of right-shifts and adds.
In practice, though, that way isn't very feasible, and even if it were it makes more practical sense to represent the reciprocal as a fixed-point constant and multiply by it.
In either case, if you aren't dividing by a constant amount (and therefore multiplying by a constant reciprocal) you're pretty much screwed.
Dan.
#28340 - isildur - Fri Oct 29, 2004 9:07 pm
Thanks, I will go with the reciprocal in fixed point format approach, since I can precalculate those in advance for what I need.