Re: calculating an exponent?

From: Jani Yusef (jani_at_persian.com)
Date: 09/28/04


Date: Tue, 28 Sep 2004 11:27:56 -0400

Alf P. Steinbach wrote:
> * Jani Yusef:
>
>>Ok, this one has me stumped.
>>Is it possible to calculate an exponent, say, a^n, using only right
>>and left shifts along with additions and subtractions?
>
>
> All arithmetic operations are implemented that way (in software or hardware).
>
> Presumably 'a' and 'n' are non-negative integers.
>
> The only question is then how (in)efficient you want the algorithm to
> be. For efficiency it might be a good idea to first consider efficient
> multiplication. With multiplication you can then tackle the efficiency of the
> exponentiation, e.g. by noting treating odd n and even n differently.

Hmmm, thats interesting but I am still confused as to how this is done?
Suppose I want to calculate 2^5 , for example. What would the
calculation look like. Would it be broken down first into
2*(2*2)*(2*2) ors omething like that? How is that different then, say,
and even exponent? Sorry for all the questions but I really don't
understand this at all.



Relevant Pages

  • Re: calculating an exponent?
    ... Jani Yusef: ... > Is it possible to calculate an exponent, say, a^n, using only right ... > and left shifts along with additions and subtractions? ... For efficiency it might be a good idea to first consider efficient ...
    (comp.programming)
  • Re: calculating an exponent?
    ... > Alf P. Steinbach wrote: ... For efficiency it might be a good idea to first consider efficient ... as before with 'n', reducing the exponent further, and so on. ...
    (comp.programming)
  • Re: calculating an exponent?
    ... > Is it possible to calculate an exponent, say, a^n, using only right ... > and left shifts along with additions and subtractions? ... private static int multiply{ ...
    (comp.programming)