473,473 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Fastest way to mulitply by 7

34 New Member
Hello all,

I got the answer that fastest way to multiply by 7 is n<<3 - n, but why?

If the question is to multiply by 8, then n<<3 is considerably better than n*8, because bitwise operation is faster. But when we multiply by 7, we are also subtracting n, it may be more cumbersome than multiplying directly with 7.

please answer,
Suyash
Jun 8 '07 #1
9 2719
weaknessforcats
9,208 Recognized Expert Moderator Expert
Consider a 1:

0001

Shift it left 3 and you have

1000

which is 8

so you sutract the number to get

0111

which is 7.

Try it with 2 which is:

00010

Shift it left 3:

10000

which is 16

So you subtract 2

01110

which is 14.

I have to say that with a mult-core 4GZ processsor, this is silly. Multiplying by shifting was popular in 1965 when a really small machine filled your bedroom.

Always nice to know how the bits work, though don't put this in your code.
Jun 8 '07 #2
Savage
1,764 Recognized Expert Top Contributor
I have to say that with a mult-core 4GZ processsor, this is silly


What is GZ,isn't that supposed to be 4GHz?

Tick,tick,tick.


Savage
Jun 8 '07 #3
JosAH
11,448 Recognized Expert MVP
I have to say that with a mult-core 4GZ processsor, this is silly. Multiplying by shifting was popular in 1965 when a really small machine filled your bedroom.

Always nice to know how the bits work, though don't put this in your code.
First I'd like to say that I don't want computers in my bedroom and second:
there are still applications for that trickery-dickery but they find use in very
small processors that lack a multiplication instruction. (even early SPARCs
multiplied like that and nowadays, say, Atmel RISC processors still like to
do it that way). For the 'when every cycle counts' world a simple multiplcation
is still pure luxury. For 4GHz, multi core or whatever larger processors, trying
to squeeze extra speed out of them using this trickery-dickery is indeed a
silly, useless exercise.

kind regards,

Jos
Jun 8 '07 #4
Suyash Upadhyay
34 New Member
Thanx...
But I wanted to ask why this method is better because subtraction bears overhead like multiplication.

Suyash
Jun 8 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
What is GZ,isn't that supposed to be 4GHz?
Silent H. :)
Jun 8 '07 #6
JosAH
11,448 Recognized Expert MVP
Thanx...
But I wanted to ask why this method is better because subtraction bears overhead like multiplication.

Suyash
As I wrote in my previous reply: suppose there is no multiplication instruction
available; using a couple of shifts and additions or subtractions would be best
or second best. Suppose there *is* a multiplication instruction available then
simply counting cycles determines which of the two variants will be best.

You simply can't say up front which one will be the best way. Nowadays big
micro processors all have a decent fast multiplication instruction available and
on average those will be faster than your alternative, but to be sure: count cycles.

kind regards,

Jos
Jun 8 '07 #7
AdrianH
1,251 Recognized Expert Top Contributor
As I wrote in my previous reply: suppose there is no multiplication instruction
available; using a couple of shifts and additions or subtractions would be best
or second best. Suppose there *is* a multiplication instruction available then
simply counting cycles determines which of the two variants will be best.

You simply can't say up front which one will be the best way. Nowadays big
micro processors all have a decent fast multiplication instruction available and
on average those will be faster than your alternative, but to be sure: count cycles.

kind regards,

Jos
A good compiler will do this for you if you set it up to optimise for speed.


Adrian
Jun 8 '07 #8
JosAH
11,448 Recognized Expert MVP
A good compiler will do this for you if you set it up to optimise for speed.


Adrian
The problem is far more complicated than people think: try to multiply 7*36
on a one byte (8 bits) processor; performing 8*36-1*36 using simple shifts
causes an overflow (8*36 == 288 which doesn't fit in a byte). Most compilers
try to play it safe so they generate 4*36+2*36+1*36 using shifts and additions.
Interchanging the multiplier and multiplicant woult've done a bit better here:
7*36 == 7*32+7*4 using shifts and additions without causing overflow.

The decision whether or not using compound instructions using two 8 bit
words and go for the 16 bit wide shifts versus using additional additions
differs per processor and can't be easily solved. Some processors, notably
the ARM/7 processor has the capability to shift 12 bit words to the left by 20
positions maximum in one single instruction including both operands.

That allows for every 32 bit word to be generated as long as the '1' bits are
no more than 11 positions apart.

Other processors have special barrel shifters in their ALU that can do other
funky things. The answer to the question 'which bit shift is best?' cannot be
easily answered. Google for 'shift for multiplication' and see the mess ;-)

kind regards,

Jos
Jun 9 '07 #9
AdrianH
1,251 Recognized Expert Top Contributor
Cool, good to know.


Adrian
Jun 9 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Simon | last post by:
Hi, If I have a string, (variable len), and I am looking for the first position of one char in array starting from position 'x' For example, // the 'haystack' $string = "PHP is great,...
9
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is...
6
by: Neal D. Becker | last post by:
I need a fairly small lookup table, and I'm wondering which data python data structure would be fastest. I could use a list, tuple, dictionary, numeric array, or maybe plain python array. The...
11
by: DraguVaso | last post by:
Hi, I should use XML to synchronize the data from different (VB.NET) applications, and I was just wondering which Overloads of these functions ( ReadXmlSchema, ReadXml and WriteXml) goes the...
4
by: laurenq uantrell | last post by:
I am trying to determine which of three stored procedure designs are fastest in the Query Analyzer: One query is a straight SELECT query with all desired rows and a dozen (tblName.RowName =...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
6
by: Klaas Vantournhout | last post by:
Hi, I have a question, which is just out of interest. What is the fastest way to do an odd/even check with c++ and if needed assembler. Assume n is an unsigned integer like type (unsigned...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: dantz | last post by:
After reading all of the materials in msdn about interprocess communication now I am confused. I hope someone can give me some enlightment. I am developing a multithreaded client-server...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.