bits and good habits

?

\\

Hello,
my question could be a dumb question, but refers to some of the
strange
tips and tricks that the C language permits (not only it).
Suppose I have to work to set a bit in a variable, based on a bit of
another variable:

if (a & 0x04)
b |= 0x02;
else
b &= ~0x02;

some programmers like to clear the bit always before:

b &= ~0x02;
if (a & 0x04)
b |= 0x02;

or even:

b &= ~0x02;
b |= (a & 0x04) >> 1;

Which is best for *efficiency* ?
There are some references on the net that discuss all these C
shortcuts
or I have to look the code around for the rest of my life? :)
Thanks,
gaetano
 
R

Rich Webb

Hello,
my question could be a dumb question, but refers to some of the
strange
tips and tricks that the C language permits (not only it).
Suppose I have to work to set a bit in a variable, based on a bit of
another variable:

if (a & 0x04)
b |= 0x02;
else
b &= ~0x02;

some programmers like to clear the bit always before:

b &= ~0x02;
if (a & 0x04)
b |= 0x02;

or even:

b &= ~0x02;
b |= (a & 0x04) >> 1;

Which is best for *efficiency* ?

The one that most "looks like" the problem you're trying to solve and
which will be the one most easily understood by the maintenance
programmer (which could be you in two years, trying to figure out why
some code snippet was written in an unusual way).

If efficiency is characterized by code space or by code speed or by some
combination of the two, first write clear, maintainable code and only
then, after you've evaluated the results against correctness and your
particular definition of efficiency, make the necessary changes.

Generally, you'll want to find a better algorithm rather than trying to
out-optimize the compiler...
 
?

\\

You are right. When I talk about efficiency, I should
specify that I am really talk about speed, but seems
that the only answer is profiling it.
Thanks for the link!
 
G

Gene

Wow. Great work.

It is nice work, but information about a specific tiny embedded
processor ought not to be used to choose a coding style. Just code it
the way it's most understandable.

You missed a possibility:

struct {
unsigned b0 : 1, b1 : 1, b2 : 1;
} a, b;

... yada yada ...

b.b1 = a.b2;
 
C

CBFalconer

\"\ said:
You are right. When I talk about efficiency, I should specify
that I am really talk about speed, but seems that the only answer
is profiling it. Thanks for the link!

Who is 'you'? What is he (or she) right about? Where did you talk
about speed? What link?

This may give you an idea what is wrong with your post. The
quoting and attribution capabilities of newsreaders are there for a
reason. There is no reason to assume your reader ever has, or ever
will, be able to read any other messages in the thread.

Some useful links on quoting:
<http://www.xs4all.nl/~wijnands/nnq/nquote.html>
<http://www.complang.tuwien.ac.at/anton/mail-news-errors.html>
<http://www.netmeister.org/news/learn2quote2.html>
<http://www.star-one.org.uk/computer/format.htm>
 
K

Keith Thompson

Walter Banks said:
HTML was the appropriate way of presenting
a readable on topic listing fragment.
[...]

Really? Why?

I didn't complain about the HTML myself because I never saw it.
Now that I take a closer look at your article, I see it was posted
as multipart/alternative, with text/plain and text/html parts.
My newsreader only showed me the text/plain part.

I suspect that some older newsreaders might not handle the
Content-Type: header, and would show both parts as raw text.

When I save the html part to a file and view it in my browser,
the only reason I can see for using HTML is that the code sample is
displayed with a fixed-width font. People post code samples here
in plain text all the time; presumably most people view them in a
fixed-width font anyway.

Plain text is perfectly adequate for Usenet. HTML is unnecessary
and can cause real problems.
 
K

Keith Thompson

Walter Banks said:
They fixed the network protocol in 1978 and the change
has made it to the servers now. You don't have to worry
anymore.

You have been up to too late once you hit midnight you turned
into a net nanny.

Why did you bother to quote the relevant portion of Chuck's article?
By your argument, it obviously wasn't necessary.
 
C

CBFalconer

blargg said:
Keith said:
Walter Banks said:
CBFalconer wrote:

The quoting and attribution capabilities of newsreaders are
there for a reason. There is no reason to assume your reader
ever has, or ever will, be able to read any other messages in
the thread.

They fixed the network protocol in 1978 and the change has
made it to the servers now. You don't have to worry anymore.
[...]
Why did you bother to quote the relevant portion of Chuck's
article? By your argument, it obviously wasn't necessary.

Being able to fetch the ENTIRE message being responded to !=
small quote of relevant portion. Since virtually all readers can
now follow the References header to find previous messages, one
needn't worry about quoting all material that might be necessary,
only that which is clearly necessary.

Following the References does not suffice. That message also has
to be present. Many people do not use purely interactive operation
with their news server. Even if they do, there is no reason for
the server to still have the message on-line. If they don't they
need to periodically clear out their own storage.

In addition, the References link is often broken. Far too many
readers don't properly truncate the linkage when it reaches 20
members. Including mine.
 
R

Richard Bos

Walter Banks said:
They fixed the network protocol in 1978 and the change
has made it to the servers now. You don't have to worry
anymore.

Sufficient experience with Usenet will prove that this is not always
true.

Richard
 
W

Walter Banks

Richard said:
Sufficient experience with Usenet will prove that this is not always
true.

I suspect that I have as much experience on this as anyone posting
to this news group. It wasn't a flippant comment.

w.
 
C

CBFalconer

Walter said:
I suspect that I have as much experience on this as anyone
posting to this news group. It wasn't a flippant comment.

Maybe you do. However, you have not observed the actualities. You
have even deleted the attributions for material you have quoted.
Observing the standards will not get you yelled at.
 
R

Richard Bos

Walter Banks said:
I suspect that I have as much experience on this as anyone posting
to this news group. It wasn't a flippant comment.

If you believe that missing posts are a thing of the past, your
experience has taught you little. I am not speaking of theoreticals.

Richard
 
C

CBFalconer

Richard said:
Walter Banks said:



Er... by its very nature, that kind of statistical information is
rather hard to collect. Not impossible, but we'd need access to at
least two news servers.

*Given* two servers, however, and assuming that they are independent
of each other, I think we can do it. Cf Polya for the details.

Doesn't work. You aren't allowing for users who operate offline.
 
A

Antoninus Twink

Your inability to acknowledge an error report that you have clearly
read does you no credit whatsoever.

Says the man who still refuses to acknowledge an error report from Han
that he has clearly read.

And I thought the Brits were meant to understand irony...
 
A

Antoninus Twink

[the usual pompous garbage]

But consider this: what you've actually achieved over the last three to
six months is to lose my respect.

Let's face it, if even a psycho like Mackintyre is starting to see
through Heathfield, you'd have to say that beyond a doubt his days as
self-appointed CLC Alpha Male are now numbered.
 
K

Kenny McCormack

[the usual pompous garbage]

But consider this: what you've actually achieved over the last three to
six months is to lose my respect.

Let's face it, if even a psycho like Mackintyre is starting to see
through Heathfield, you'd have to say that beyond a doubt his days as
self-appointed CLC Alpha Male are now numbered.

Indeed.
 
K

Kenny McCormack

Mark McIntyre said:
You have failed to correctly read the requirements definition, and have
therefore overengineered the solution.

I see this all the time in certain types of software engineer. They fail
to understand the difference between delivering what the customer
requested, and delivering what /they/ would want if /they/ were the
customer, or what they /think/ the customer /ought/ to be asking for.

But that's what this newsgroup is all about - you do remember which
newsgroup you are posting to, don't you?

You've analyzed the situation very well - and have quite rightly
referred to the situation at hand as "over-engineering". I like that
word. But this *is* CLC - and CLC is all about never deigning to solve
the problem at hand, but instead going off on some weird tangent of the
poster's fancy - often in the name of 199% portability (almost never a
requirement in the real world - yes, portability is good, but 199%
portability in the face of all other requirements - primo of all being
cost - is silly). Everybody always over-engineers, so as to make the
solution useless to the solution-requester.
 
G

Guest

Mark McIntyre said:



Poorly.

oh if the idea is to produce *some* stats then I can do much better
than Mark.

9. There are 9 posts missing.

Quick to calculate and requires no programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top