Velocity Reviews > bits and good habits

# bits and good habits

\\
Guest
Posts: n/a

 05-01-2009
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

Rich Webb
Guest
Posts: n/a

 05-01-2009
On Fri, 1 May 2009 10:11:37 -0700 (PDT), "\"\"" <(E-Mail Removed)>
wrote:

>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...

--
Rich Webb Norfolk, VA

\\
Guest
Posts: n/a

 05-02-2009
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.

\\
Guest
Posts: n/a

 05-02-2009
Wow. Great work.

Gene
Guest
Posts: n/a

 05-02-2009
On May 2, 4:39*pm, "\"\"" <(E-Mail Removed)> wrote:
> 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;

b.b1 = a.b2;

CBFalconer
Guest
Posts: n/a

 05-03-2009
\"\" wrote:
>
> 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

This may give you an idea what is wrong with your post. The
reason. There is no reason to assume your reader ever has, or ever

<http://www.xs4all.nl/%7ewijnands/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>

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

CBFalconer
Guest
Posts: n/a

 05-03-2009
Walter Banks wrote:
>
> Part 1.1 Type: Plain Text (text/plain)
> Encoding: 7bit

You are posting in html. Please don't.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

Keith Thompson
Guest
Posts: n/a

 05-03-2009
Walter Banks <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"

Keith Thompson
Guest
Posts: n/a

 05-03-2009
Walter Banks <(E-Mail Removed)> writes:
> CBFalconer wrote:
>
>> The
>> 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.
>
> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"

CBFalconer
Guest
Posts: n/a

 05-03-2009
blargg wrote:
> Keith Thompson wrote:
>> Walter Banks <(E-Mail Removed)> writes:
>>> CBFalconer wrote:
>>>
>>>> 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
>>>
>>> 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
> 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.

members. Including mine.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>