Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > bits and good habits

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
\\
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.
Thanks for the link!
 
Reply With Quote
 
\\
Guest
Posts: n/a
 
      05-02-2009
Wow. Great work.
 
Reply With Quote
 
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;

... yada yada ...

b.b1 = a.b2;
 
Reply With Quote
 
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
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/%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>
Try the download section.


 
Reply With Quote
 
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>
Try the download section.


 
Reply With Quote
 
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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-03-2009
Walter Banks <(E-Mail Removed)> writes:
> 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.
>
> 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"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-03-2009
blargg wrote:
> Keith Thompson wrote:
>> Walter Banks <(E-Mail Removed)> writes:
>>> 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.

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
100 good unix habits Aquilegia Alyssum NZ Computing 14 12-20-2006 08:18 AM
Aspect Ratio and photo album habits ivondale@aol.com Digital Photography 9 07-24-2006 08:50 PM
Need "Proof" To Give to Co-worker About Browsing Habits & Spyware/Adware/Torjans/Viruses Sens Fan Happy In Ohio Computer Support 26 09-23-2005 07:09 PM
IE interpretation habits... Nils Weisensee HTML 3 03-06-2005 09:16 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM



Advertisments