bits and good habits

Discussion in 'C Programming' started by \\, May 1, 2009.

  1. \\

    \\ Guest

    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
     
    \\, May 1, 2009
    #1
    1. Advertising

  2. \\

    Rich Webb Guest

    On Fri, 1 May 2009 10:11:37 -0700 (PDT), "\"\"" <>
    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
     
    Rich Webb, May 1, 2009
    #2
    1. Advertising

  3. \\

    \\ Guest

    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!
     
    \\, May 2, 2009
    #3
  4. \\

    \\ Guest

    Wow. Great work.
     
    \\, May 2, 2009
    #4
  5. \\

    Gene Guest

    On May 2, 4:39 pm, "\"\"" <> 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;
     
    Gene, May 2, 2009
    #5
  6. \\

    CBFalconer Guest

    \"\" 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.
     
    CBFalconer, May 3, 2009
    #6
  7. \\

    CBFalconer Guest

    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.
     
    CBFalconer, May 3, 2009
    #7
  8. Walter Banks <> 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.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, May 3, 2009
    #8
  9. Re: More Net Nanny

    Walter Banks <> 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) <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, May 3, 2009
    #9
  10. \\

    CBFalconer Guest

    Re: More Net Nanny

    blargg wrote:
    > Keith Thompson wrote:
    >> Walter Banks <> 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.
     
    CBFalconer, May 3, 2009
    #10
  11. \\

    Richard Bos Guest

    Re: More Net Nanny

    Walter Banks <> wrote:

    > CBFalconer wrote:
    >
    > > 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.


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

    Richard
     
    Richard Bos, May 6, 2009
    #11
  12. \\

    Walter Banks Guest

    Re: More Net Nanny

    Richard Bos wrote:

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


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

    w.
     
    Walter Banks, May 6, 2009
    #12
  13. \\

    CBFalconer Guest

    Re: More Net Nanny

    Walter Banks wrote:
    > Richard Bos wrote:
    >
    >>> 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.

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

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, May 6, 2009
    #13
  14. \\

    Richard Bos Guest

    Re: More Net Nanny

    Walter Banks <> wrote:

    > Richard Bos wrote:
    >
    > > > 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.

    >
    > 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
     
    Richard Bos, May 9, 2009
    #14
  15. \\

    CBFalconer Guest

    Re: More Net Nanny

    Richard Heathfield wrote:
    > Walter Banks said:
    >
    > <snip>
    >
    >> I would love to see hard data that shows missing posts stats.

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

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, May 10, 2009
    #15
  16. Re: More Net Nanny

    On 10 May 2009 at 1:07, Richard Heathfield wrote:
    > 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...
     
    Antoninus Twink, May 10, 2009
    #16
  17. Re: More Net Nanny

    On 9 May 2009 at 23:05, Mark McIntyre wrote:
    > On 10/05/09 00:04, Richard Heathfield wrote:
    >> [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.
     
    Antoninus Twink, May 10, 2009
    #17
  18. Re: More Net Nanny

    In article <>,
    Antoninus Twink <> wrote:
    >On 9 May 2009 at 23:05, Mark McIntyre wrote:
    >> On 10/05/09 00:04, Richard Heathfield wrote:
    >>> [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.
     
    Kenny McCormack, May 10, 2009
    #18
  19. Re: More Net Nanny

    In article <ouINl.275141$1.easynews.com>,
    Mark McIntyre <> wrote:
    ....
    >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.
     
    Kenny McCormack, May 10, 2009
    #19
  20. \\

    Guest

    Re: More Net Nanny

    On 10 May, 01:55, Richard Heathfield <> wrote:
    > Mark McIntyre said:
    >
    > <snip>
    >
    > > The requirement was to produce some stats on missing posts. The
    > > method I outlined meets this objective.

    >
    > 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.
     
    , May 11, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nils Weisensee

    IE interpretation habits...

    Nils Weisensee, Mar 4, 2005, in forum: HTML
    Replies:
    3
    Views:
    436
    Jan Faerber
    Mar 6, 2005
  2. Carroll, Barry
    Replies:
    7
    Views:
    300
  3. Carroll, Barry
    Replies:
    7
    Views:
    259
    Dennis Lee Bieber
    Jan 20, 2007
  4. Carroll, Barry
    Replies:
    2
    Views:
    264
    Neil Cerutti
    Jan 20, 2007
  5. Replies:
    32
    Views:
    1,049
    Andrea Francia
    Nov 10, 2008
Loading...

Share This Page