455,431 Members | 1,845 Online
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

# what is problem ?

 P: n/a suppose int a=4; int prod1=17*a; int prod2=a<<4+a; //another way of implementing 17*a Is there any problem with prod2..? Nov 13 '08 #1
7 Replies

 P: n/a On Nov 13, 5:23 pm, "c.lang.mys...@gmail.com"

 P: n/a On Nov 13, 11:23*pm, "c.lang.mys...@gmail.com"

 P: n/a c.***********@gmail.com wrote: suppose int a=4; int prod1=17*a; (We must be inside a function.) int prod2=a<<4+a; //another way of implementing 17*a Is there any problem with prod2..? Even if it's fixed as vippstar or Zhao note elsethread, the problem with `prod2` is that it's not clear what's going on. The Horse! Why shift-and-add when you can multiply by 17 and have the compiler sort things out? -- "We dance, and the worlds melt away." - Curved Air, /Metamorphosis/ Hewlett-Packard Limited registered no: registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England Nov 13 '08 #4

 P: n/a vi******@gmail.com writes: On Nov 13, 5:23 pm, "c.lang.mys...@gmail.com" supposeint a=4;int prod1=17*a;int prod2=a<<4+a; //another way of implementing 17*aIs there any problem with prod2..? There's no problem, the problem is the comment which is a lie. a << 4 + a means a << (4 + a) I think you just did his homework for him. To the original poster: Why do you ask? Did you try running the code yourself and seeing what it does? -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Nov 13 '08 #5

 P: n/a Chris Dollin wrote: c.***********@gmail.com wrote: >supposeint a=4;int prod1=17*a; (We must be inside a function.) >int prod2=a<<4+a; //another way of implementing 17*aIs there any problem with prod2..? Even if it's fixed as vippstar or Zhao note elsethread, the problem with `prod2` is that it's not clear what's going on. The Horse! Why shift-and-add when you can multiply by 17 and have the compiler sort things out? Well, if they turn out to be non-equivalent, how do you know that the OP needs specifically the former and not the latter? BTW, they are not equivalent. The latter produces undefined behavior for negative 'a' in C99. It is, of course, rather strange to allow the possibility of OP preferring the UB for negative input, but nevertheless... :) -- Best regards, Andrey Tarasevich Nov 13 '08 #6

 P: n/a c.***********@gmail.com wrote: int prod2=a<<4+a; //another way of implementing 17*a Why so complicated? Read this

 P: n/a Wolfgang Draxinger wrote: c.***********@gmail.com wrote: >int prod2=a<<4+a; //another way of implementing 17*a Why so complicated? Read this Try the download section. Nov 14 '08 #8

### This discussion thread is closed

Replies have been disabled for this discussion.