By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
7 Replies


P: n/a
On Nov 13, 5:23 pm, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:
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..?
There's no problem, the problem is the comment which is a lie.

a << 4 + a means a << (4 + a)
Nov 13 '08 #2

P: n/a
On Nov 13, 11:23*pm, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:
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..?
It should be "int prod2 = (a << 4) + a;"
Nov 13 '08 #3

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"
<c.lang.mys...@gmail.comwrote:
>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..?

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 <http://www.ghoti.net/~kst>
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:
>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?
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
<http://dl.fefe.de/optimizer-isec.pdfto get some insight, why
writing readable code should be preferred over "hand optimized"
code, which in some cases even performs worse.

Wolfgang Draxinger
--
E-Mail address works, Jabber: he******@jabber.org, ICQ: 134682867

Nov 13 '08 #7

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
<http://dl.fefe.de/optimizer-isec.pdfto get some insight, why
writing readable code should be preferred over "hand optimized"
code, which in some cases even performs worse.
In addition, it doesn't work. It runs into either implementation
defined or undefined results whenever a is a negative integral
quantity.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Nov 14 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.