Bonus Puzzle (spoiler)

Wyverex wyverex.cypher at gmail.com
Wed Aug 6 16:07:09 PDT 2008


Wyverex wrote:
> Through another one in....
> 
> 
> 
> (Multiply x by 7 without using multiplication (*) operator.)
> 
> Bonus Problem!
> Multiply X by Y without using multiplication (*) operator..  Faster the 
> better!!!


import tango.io.Stdout;

uint mult ( in uint a, in uint b )
{
    uint shift, ans;

    while( b )
    {
       ans += ( b & 1 ? a << shift : 0 );
       shift ++;
       b >>= 1;
    }
    return ans;
}

void main()
{
   uint x = 92;
   uint y = 478;

   Stdout( x * y ).newline;  //here to prove value is right!!
   Stdout( mult(x, y) ).newline;
}


More information about the Digitalmars-d-learn mailing list