Velocity Reviews > multiplication by 7

# multiplication by 7

sudharsan
Guest
Posts: n/a

 02-23-2006
give a fast way to multiply a number by 7

pemo
Guest
Posts: n/a

 02-23-2006
sudharsan wrote:
> give a fast way to multiply a number by 7

int n = 1;

n *= 7; // Just a few clocks on my system!

--
==============
*Not a pedant*
==============

Guest
Posts: n/a

 02-23-2006

sudharsan wrote:
> give a fast way to multiply a number by 7

Think along the lines of:

x*7 = x*(4+2+1)

How is this related to C?

--

Tim Prince
Guest
Posts: n/a

 02-23-2006
Vladimir S. Oka wrote:
> sudharsan wrote:
>
>>give a fast way to multiply a number by 7

>
>
> Think along the lines of:
>
> x*7 = x*(4+2+1)
>
> How is this related to C?
>

Most C compilers know a reasonable compromise between generated code
size and speed for their target architecture.

Guest
Posts: n/a

 02-23-2006

Tim Prince wrote:
> Vladimir S. Oka wrote:
> > sudharsan wrote:
> >
> >>give a fast way to multiply a number by 7

> >
> >
> > Think along the lines of:
> >
> > x*7 = x*(4+2+1)
> >
> > How is this related to C?
> >

> Most C compilers know a reasonable compromise between generated code
> size and speed for their target architecture.

I know that. The OP's question is obviously a homework or some-such. If
that's the case it'll still be useful for him to try and implement the
above. For good measure, I probably should have added The First Rule of
Optimisation:

1. DON'T

Other rules, for more advanced users can be found elsegroup.

--

pemo
Guest
Posts: n/a

 02-23-2006
Vladimir S. Oka wrote:
> Tim Prince wrote:
>> Vladimir S. Oka wrote:
>>> sudharsan wrote:
>>>
>>>> give a fast way to multiply a number by 7
>>>
>>>
>>> Think along the lines of:
>>>
>>> x*7 = x*(4+2+1)
>>>
>>> How is this related to C?
>>>

>> Most C compilers know a reasonable compromise between generated code
>> size and speed for their target architecture.

>
> I know that. The OP's question is obviously a homework or some-such.
> If that's the case it'll still be useful for him to try and implement
> the above. For good measure, I probably should have added The First
> Rule of Optimisation:
>
> 1. DON'T
>
> Other rules, for more advanced users can be found elsegroup.

If you're thinking of 'shifting' - how's that going to work with floats?

--
==============
*Not a pedant*
==============

Guest
Posts: n/a

 02-23-2006

pemo wrote:
> Vladimir S. Oka wrote:
> > Tim Prince wrote:
> >> Vladimir S. Oka wrote:
> >>> sudharsan wrote:
> >>>
> >>>> give a fast way to multiply a number by 7
> >>>
> >>>
> >>> Think along the lines of:
> >>>
> >>> x*7 = x*(4+2+1)
> >>>
> >>> How is this related to C?
> >>>
> >> Most C compilers know a reasonable compromise between generated code
> >> size and speed for their target architecture.

> >
> > I know that. The OP's question is obviously a homework or some-such.
> > If that's the case it'll still be useful for him to try and implement
> > the above. For good measure, I probably should have added The First
> > Rule of Optimisation:
> >
> > 1. DON'T
> >
> > Other rules, for more advanced users can be found elsegroup.

>
> If you're thinking of 'shifting' - how's that going to work with floats?

I was, yes. It won't, but I think the answer was at least as good as
the question.

I could have offered 42 as well, choosing the `number` to be 6.

--

pablo reda
Guest
Posts: n/a

 02-23-2006
x*7 = (x<<2)+(x<<1)+x

Lew Pitcher
Guest
Posts: n/a

 02-23-2006
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sudharsan wrote:
> give a fast way to multiply a number by 7

P'haps you are looking for something other than
number = number * 7;

If so, then you could use shifts and additions

In algebra, you learn that
N * 7 = N * (4 + 2 + 1)
= (N * 4) + (N * 2) + (N * 1)

In binary maths, multiplication by a power of two can be accomplished with an
integer number of left shifts
N * 4 = N << 2
N * 2 = N << 1
N * 1 = N << 0

Substituting for common values gives
N * 7 = (N << 2) + (N << 1) + (N << 0)

Inverting order for algorithmic efficiency gives
N * 7 = (N << 0) + (N << 1) + (N << 2)

You can code this as
unsigned int number = 3;

number = (number) + (number << 1) + (number << 2);

But, this is likely to be /less/ efficient than
number = number * 7;

- --
Lew Pitcher
IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)

iD8DBQFD/dJtagVFX4UWr64RAspvAKCtNLjd2mWqpBTcvHuV5P/Ut1PuiwCg3WdJ
+81jNXo/S08/426EtxBDK3Y=
=I4ET
-----END PGP SIGNATURE-----

Tim Prince
Guest
Posts: n/a

 02-23-2006
pablo reda wrote:
> x*7 = (x<<2)+(x<<1)+x
>

slow on P4