473,657 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A couple questions

Hello,

First question: besides the syntactical difference of one having
to be enclosed in brackets in some situations, are there any subtler
differences between (say)
x=(some expression), y=(some expression);
and
x=(some expression); y=(some expression); ?

What about sequence points? For example, would the phrase "i++, i++;"
be well-defined? Is there any guarantee about which computation the
program will perform first, for example is "x=x+1, x=x*2;" a
well-defined phrase? (intuitively I would assume so and that it would
go left to right but I'd like to be sure)
Second question: in one project I maintain, the guy is fond of
using >and << for routine division/multiplication. For instance
instead of size *= 2, he will write size <<= 1. My question is, is
there a practical reason for doing this, for routine situations besides
actual bit twiddling? I know << is usually faster than *, but I always
assumed any compiler worth a hill of beans would easily know to
optimize something like "size *= 2" as much as possible anyway?

Thanks for helping out, you guys rock =)

-Snis P.

Jul 27 '06 #1
7 1179
On 2006-07-27, Snis Pilbor <sn********@yah oo.comwrote:
Hello,

First question: besides the syntactical difference of one having
to be enclosed in brackets in some situations, are there any subtler
differences between (say)
x=(some expression), y=(some expression);
and
x=(some expression); y=(some expression); ?
Well, the first is a single expression returning the value of y. The
second is two separate expressions and can't be used as, say,
c = (x=a, y=b); which gives c the value of b.
What about sequence points? For example, would the phrase "i++, i++;"
be well-defined? Is there any guarantee about which computation the
program will perform first, for example is "x=x+1, x=x*2;" a
well-defined phrase? (intuitively I would assume so and that it would
go left to right but I'd like to be sure)
I believe that it is well-defined. However, I don't think that the order
of computations is guaranteed. I don't claim to be an expert, though, so
you should wait for a more knowledgable person to answer.

I do know, however, that most well-written code shouldn't care what the
order of computations is, because a maintenance programmer shouldn't be
expected to know. Well-written code should be clear and be evaluated in
one (obvious) way.
Second question: in one project I maintain, the guy is fond of
using >and << for routine division/multiplication. For instance,
instead of size *= 2, he will write size <<= 1. My question is, is
there a practical reason for doing this, for routine situations besides
actual bit twiddling?
Tsk, tsk, tsk. There is no reason for that, except to make the code
unreadable. Tomorrow I might want to multiply by 12 instead of 8; with
multiplication that involves changing one number.

y *= 8; /* In the case that the grommet fails to widge consistently,
multiply by 13 to compensate. Shouldn't be an issue with
the new vendor, though. */
x <<= 2; /* If we're interfacing with DeviceBoy revision F, we need to
shift by 4 to make room for the LCD brightness flags. We
should be compatible before the next version ships. */

See how using an inappropriate operator would make maintenance more
difficult? You should correct your errant colleague.
I know << is usually faster than *
No you don't.
, but I always
assumed any compiler worth a hill of beans would easily know to
optimize something like "size *= 2" as much as possible anyway?
Indeed it will.
Thanks for helping out, you guys rock =)
No problem. :-)

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 1 days
Jul 27 '06 #2


Snis Pilbor wrote On 07/27/06 14:49,:
Hello,

First question: besides the syntactical difference of one having
to be enclosed in brackets in some situations, are there any subtler
differences between (say)
x=(some expression), y=(some expression);
and
x=(some expression); y=(some expression); ?
Well, you can throw parentheses around the first one
(not enclosing the semicolon) and use it as part of a
still larger expression:

z = (x = some_expression , y = some_expression ) + 42;

.... which you could not do with a pair of complete statements.
What about sequence points?
There are sequence points at the comma and at the
semicolons.

For example, would the phrase "i++, i++;"
be well-defined?
Yes.
Is there any guarantee about which computation the
program will perform first, for example is "x=x+1, x=x*2;" a
well-defined phrase? (intuitively I would assume so and that it would
go left to right but I'd like to be sure)
You assume correctly. However, the fact that you find it
necessary to assume is troubling; *all* this stuff should be
in your C textbook or reference or whatever. If you don't
have such a thing, you should.
Second question: in one project I maintain, the guy is fond of
using >and << for routine division/multiplication.
Oh, no! Again? Didn't we just *do* this? Didn't we just
do this for the umpteenth time? Go Google it: just pick any
ten threads at random and you'll have a better-than-even chance
of seeing this particular Apple of Discord on one or another of
its all-too-frequent trips down the table.

--
Er*********@sun .com

Jul 27 '06 #3
On Thu, 27 Jul 2006, Andrew Poelstra wrote:
On 2006-07-27, Snis Pilbor <sn********@yah oo.comwrote:
>What about sequence points? For example, would the phrase "i++, i++;"
be well-defined? Is there any guarantee about which computation the
program will perform first, for example is "x=x+1, x=x*2;" a
well-defined phrase? (intuitively I would assume so and that it would
go left to right but I'd like to be sure)

I believe that it is well-defined. However, I don't think that the order
of computations is guaranteed. I don't claim to be an expert, though, so
The order is guaranteed. However, be careful not to confuse
the comma operator with comma-separated lists.

Tak-Shing
Jul 27 '06 #4
Snis Pilbor wrote:
Hello,

First question: besides the syntactical difference of one having
to be enclosed in brackets in some situations, are there any subtler
differences between (say)
x=(some expression), y=(some expression);
The above returns a value. So you could do:
z = x=(some expression), y=(some expression);
and
x=(some expression); y=(some expression); ?
Obviously this is different.
What about sequence points? For example, would the phrase "i++, i++;"
be well-defined? Is there any guarantee about which computation the
program will perform first, for example is "x=x+1, x=x*2;" a
well-defined phrase? (intuitively I would assume so and that it would
go left to right but I'd like to be sure)
The comma operator (as opposed to the comma used to separate arguments
passed to a function) is a sequence point and guarantees the left
argument is evaluated first.
Second question: in one project I maintain, the guy is fond of
using >and << for routine division/multiplication. For instance
instead of size *= 2, he will write size <<= 1. My question is, is
there a practical reason for doing this, for routine situations besides
actual bit twiddling? I know << is usually faster than *, but I always
assumed any compiler worth a hill of beans would easily know to
optimize something like "size *= 2" as much as possible anyway?
Many years ago there was sometimes an advantage. However, for a long
time compilers have been intelligent enough to use a shift where it is
appropriate (sometimes doing a multiply will be faster).

Using a shift, on the other hand, has a number of disadvantages. Right
shifting a negative number will not do a division by a power of two on
*all* systems. Shifting when you intend multiplication/division is less
clear to subsequent human readers.

So I would *always* use multiplication or division when that is what I
mean unless it is *proved* to be a problem in that specific instance.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jul 27 '06 #5
Snis Pilbor wrote:
...
First question: besides the syntactical difference of one having
to be enclosed in brackets in some situations, are there any subtler
differences between (say)
x=(some expression), y=(some expression);
and
x=(some expression); y=(some expression); ?
Assuming that both are complete statements (they end in ';' in your example),
there's no difference between the two.
What about sequence points?
',' operator produces a sequence point, so there's no problem here.
For example, would the phrase "i++, i++;"
be well-defined?
Yes.
Is there any guarantee about which computation the
program will perform first, for example is "x=x+1, x=x*2;" a
well-defined phrase?
The left-hand side is evaluated first. As dictated by the sequence point, all
side-effects of the LHS take place before the evaluation of RHS begins.
(intuitively I would assume so and that it would
go left to right but I'd like to be sure)
That's indeed how it would go.
Second question: in one project I maintain, the guy is fond of
using >and << for routine division/multiplication. For instance
instead of size *= 2, he will write size <<= 1. My question is, is
there a practical reason for doing this, for routine situations besides
actual bit twiddling?
No, no reason (unless you are using a really weird compiler).

Also note that for negative left operand ('size') the effects of 'size >1' and
'size / 2' are not necessarily identical. In C90 both are
implementation-defined. In C99 the latter is required to round toward 0, while
the former is still implementation-defined.
I know << is usually faster than *,
No. '<<' and '*' are C language operators. The notion of 'speed' is not
applicable to them. Apparently, you are identifying these operators with
"similar" CPU instructions of you favorite hardware platform (say, 'shl' and
'mul'), believing that there's a 1:1 match between them. This is not correct in
general case. When it comes to such simple expressions, modern compilers usually
easily recognize equivalent expressions and generate identical (optimal) machine
instructions for them, i.e. 'size *= 2' and 'size <<= 1' will normally produce
the same code and give you the same efficiency. What often comes as a surprise
to such manual-optimization fans is that the most optimal code for both 'size *=
2' and 'size <<= 1' does not use neither CPU's multiply operation nor CPU's
shift operation, but instead uses something much less obvious, like some form of
CPU's load-effective-address operation.

--
Best regards,
Andrey Tarasevich
Jul 27 '06 #6
>Snis Pilbor wrote:
>... are there any subtler differences between (say)
x=(some expression), y=(some expression);
In article <0n************ @news.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.ukwro te:
>The above returns a value. So you could do:
z = x=(some expression), y=(some expression);
Yes -- but note that:

z = x = 4, y = 5;

parses as:

(z = (x = 4)), (y = 5);

since the "=" operator binds more tightly than the "," operator.
>and x=(some expression); y=(some expression); ?
>Obviously this is different.
In this case, it would give the same result:

z = x = 4; y = 5;

still sets both z and x to 4, and y to 5.
>What about sequence points? ...
>The comma operator (as opposed to the comma used to separate arguments
passed to a function) is a sequence point and guarantees the left
argument is evaluated first.
Yes. However, if (x = 4, y = 5) is parenthesized and the entire
thing is put into a larger expression, one can obtain undefined
behavior:

x = x + (x = 4, y = 5); /* undefined */

The version with the semicolons is not an expression, so it cannot
be parenthesized and used as one.
> Second question: in one project I maintain, the guy is fond of
using >and << for routine division/multiplication. ...
>Many years ago there was sometimes an advantage. However, for a long
time compilers have been intelligent enough to use a shift where it is
appropriate (sometimes doing a multiply will be faster).
Even Dennis Ritchie's original PDP-11 C compiler would turn "x *
constant-power-of-2" into "x << log2(that)", if I remember right.
Modern compilers are pretty good at this; "x = (x * 10) + (c -
'0')" turns into a pair of non-obvious VAX instructions under GCC:

# if x is in r3 and c is in r2
moval r3[r3], r3
movaw -48(r2)[r3], r3
>Using a shift, on the other hand, has a number of disadvantages. Right
shifting a negative number will not do a division by a power of two on
*all* systems. Shifting when you intend multiplication/division is less
clear to subsequent human readers.

So I would *always* use multiplication or division when that is what I
mean unless it is *proved* to be a problem in that specific instance.
You can also help out the compiler by using "unsigned" if the value
will never be negative:

unsigned int x;
...
x /= 32;

usually allows the compiler to emit "x >>= 5" internally, because
here a shift *does* give the same answer as a divide.

(GCC will still replace divides when appropriate: some turn into
shift-and-fix-up, others turn into reciprocal multiply.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 28 '06 #7
Andrew Poelstra <ap*******@poel stra01.lanwrote :

(WRT to x=x+1, x=x*2;)
I believe that it is well-defined. However, I don't think that the order
of computations is guaranteed. I don't claim to be an expert, though, so
you should wait for a more knowledgable person to answer.
You are correct on the first point; however, the order of computations
is guaranteed, at least per 6.5.17 of n869:

"The left operand of a comma operator is evaluated as a void expression;
there is a sequence point after its evaluation. Then the right operand is
evaluated; the result has its type and value."

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Jul 28 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3065
by: bjs | last post by:
I hope I've got the right group here and apologies if I haven't. I'm a hobby programmer and have just upgraded to Visual Basic.net and have a couple of questions if anybody can help. In VB 6 you could print using a print command that included Print, Line, CurrentX, CurrentY etc etc. I am SURE there will be a way in VB.net yet can find no trace - can somebody help? Secondly, I know this is old tech but using DAO I could write programs...
1
1567
by: hoochiegooch | last post by:
Hi, all. I have a couple of questions about NAnt. 1. Is there a better way to invoke NAnt from a C# "Master Test Rig" than shelling out? IOW, how should an ASP.NET web app or Windows Service kick off a NAnt build session? 2. We have some test tools that can
21
3835
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
1
1485
by: dln | last post by:
Howdy. I'm a bit new to C# and got a couple of quick questions that perhaps someone can help me answer. First, is there a property that you set on a TextBox control that will force the control to accept only numerical data (i.e. - only numbers 0 to 9, a decimal and the negative sign)? So far, I've had to do the validation manually on either a key down or OnValidate event. Secondly, is there any way to preview a key stroke for a given...
4
1188
by: Sccr18 | last post by:
I just have a couple of easy questions: 1. In Asp.net using Vb is there a way to list all the possible characters without listing them all like Char() = "abc..."? 2. I was trying to set the focus to a text and was not able to by using txtEmp.setfocus(). I read somewhere that this may not be possible, is that true? 3. When you sign on to my web page that I am making and go to type something into the textboxes, dropdown boxes with...
3
1115
by: punt | last post by:
I've got a couple of questions about a deployed VB.NET application. Firstly can a deployed application remember any variables(single string) after being closed down and re-opened, without the use of a database? Secondly I've been looking at inserting bookmarks into a word template like shown in: http://support.microsoft.com/default.aspx?scid=kb;EN-US;316383 Is it possible to insert an image into a bookmark as well, or does it only work...
0
505
by: Marc | last post by:
Hi, I'm working with a customer that is trying to find a solution to provide geographical redundancy. Our application is currently using IBM DB2 v8.2. I have a couple of questions with regards to HADR: 1. DB2 HADR (Can it be used to support geographical redundancy in a nested fashion (ie. Synchronous HADR at a site, and ASYNCRONOUS between sites using the same instances/databases). If so how
3
1590
by: Ron | last post by:
Hello everyone, I have a couple of questions really quick questions. I am creating a dispatch database. What I would really like to do is have to option of automatically inserting the time when I hit the "]" button. Is this possble? Also I am trying to create a form that has the standard boxes contained within it and I would also like to add a subform or something simular in
3
1100
by: melton9 | last post by:
I'm just getting into using datagrid and have a couple of questions. 1.)How do you get the grid to show the values of a datatable automatically? Currently I have to hit the + sign and then select "getNews"(the datatable) to show my data. 2.) I have this set up to a loop. So after I view my grid any updates in the loop causes the datagrid to go blank(ie gray box). Anybody know what I should add to make the datagrid correctly
0
1261
by: Newish | last post by:
Hi Couple of questions on datagrid 1) Is there a performance issue when using datagrid to display data from a datatable. 2) Is there a security issue when using datagrid to display data from a datatable and enable it to allow edits as well.
0
8395
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6166
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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 we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.