How to multiply a varible x (int) by 7, without using multiplication & addition ?

Showing Answers 1 - 15 of 15 Answers

jh_xu

  • Nov 23rd, 2006
 

there was a type in my previous answer. It should be: (x << 3) - x

kalayama

  • Dec 5th, 2006
 

I have a suggestion here. What have you gained by doing a simple multiplicaton that way? AT the end of the day, there has got to be some value addition by what we do. doing something simply because we can sounds rather stupid to me.May be you have a valid answer. Why on earth would anyone do multiplication this way? (Such a waste of an effort as the solution can't evn be generalised!).Can anyone find how to multiply x (int) WITH ALL DIGITS from 2-9? (Our dear friend has given an answer for multiplying using 7).Could you please explain what is the necessity for not using * or +? Or is there any value addition (In terms of RAM usage or speed of execution)?Sorry If I sound rude here, I was just curious why would we want such thing?If it is just a BrainTeaser in C++, may be we should place it under BrainTeaser tag.

  Was this answer useful?  Yes

This can be a generic solution for x*y without using *.

while(y>>=1){
if(y | 1) tot+=x; x<<=1; }

or,

for( ; y ; y>=1, x<<=1 ) if(y | 1) tot+=x;

or even stupid like, while(x--) tot+=y;        ;-)

May be a brain teaser, but this may eat time.

  Was this answer useful?  Yes

bapu_60

  • Feb 20th, 2008
 

multiplication is a costly operation,costlier than bit operations.So,it is a better practice to use bit operation that is << for multiplication and >> for divsion when you are multiplying with a factor of 2 otherwise its not suggested. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions