473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what is problem ?

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 1317
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
86
by: Michael Kalina | last post by:
Because when I asked for comments on my site-design (Remember? My site, your opinion!) some of you told me never to change anything on font-sizes! What do you guys think of that:...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
4
by: Trond Meistad | last post by:
I have a website where I run a simple asp.net web application. On Sunday night, requests to this webapplication started to time out. After much debuggeing with no result, I created a new...
17
Banfa
by: Banfa | last post by:
I felt that this was a good point to start a tutorial on C/C++ programming because clearly we need to have some idea of what we are trying to achieve before we start out. I recently found this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.