Does bit operation always work more efficiently than math operation?

Discussion in 'C Programming' started by david ullua, Feb 28, 2006.

  1. david ullua

    david ullua Guest

    Hi,
    In Expand.c of BSD system, I met the following codes, the expression
    (column & 07) if used to compare variable column and 7, if column<=7,
    it returns true, else false. It use (column & 07) rather than
    (column<=7), thus it brings me a question, does bit operation always
    work more efficiently than math operation? How about plus without
    carry? and minus without borrow?
    I have searched through google web and newsgroups, haven't found any
    relative topics :(

    int column = 0;
    //other codes .....
    do {
    putchar(' ');
    column++;
    } while (column & 07);
    david ullua, Feb 28, 2006
    #1
    1. Advertising

  2. david ullua

    Thad Smith Guest

    david ullua wrote:
    > Hi,
    > In Expand.c of BSD system, I met the following codes, the expression
    > (column & 07) if used to compare variable column and 7, if column<=7,
    > it returns true, else false. It use (column & 07) rather than
    > (column<=7), thus it brings me a question, does bit operation always
    > work more efficiently than math operation? How about plus without
    > carry? and minus without borrow?
    >
    > int column = 0;
    > //other codes .....
    > do {
    > putchar(' ');
    > column++;
    > } while (column & 07);


    The bitwise and operator allows the do loop to perform a generalized tab
    operation, assuming stops every 8 characters. It also works if column >
    7, while a simple compare wouldn't. Perhaps in this case, column never
    exceeds 7 (depending on what "other code"" includes), in which case a
    compare would be functionally equivalent. C doesn't dictate the
    relative efficiency, but on most processors both forms would be very
    simple and efficient.

    --
    Thad
    Thad Smith, Feb 28, 2006
    #2
    1. Advertising

  3. david ullua

    Neil Guest

    david ullua wrote:
    > Hi,
    > In Expand.c of BSD system, I met the following codes, the expression
    > (column & 07) if used to compare variable column and 7, if column<=7,
    > it returns true, else false. It use (column & 07) rather than
    > (column<=7), thus it brings me a question, does bit operation always
    > work more efficiently than math operation? How about plus without
    > carry? and minus without borrow?
    > I have searched through google web and newsgroups, haven't found any
    > relative topics :(
    >
    > int column = 0;
    > //other codes .....
    > do {
    > putchar(' ');
    > column++;
    > } while (column & 07);
    >


    It would depend on the CPU. The Compiler converts the C to opcodes.
    CPUs have a different assortment of opcodes. So what is "better" on one
    machine is worse on another. Or, exactly the same.

    It is the Compilers optimizers job to minimize the effect.
    Neil, Feb 28, 2006
    #3
  4. david ullua

    Guest

    david ullua wrote:
    > In Expand.c of BSD system, I met the following codes, the expression
    > (column & 07) if used to compare variable column and 7, if column<=7,
    > it returns true, else false. It use (column & 07) rather than
    > (column<=7), thus it brings me a question, does bit operation always
    > work more efficiently than math operation? How about plus without
    > carry? and minus without borrow?
    > I have searched through google web and newsgroups, haven't found any
    > relative topics :(
    >
    > int column = 0;
    > //other codes .....
    > do {
    > putchar(' ');
    > column++;
    > } while (column & 07);


    Bit operations are not necessarily always more efficient than
    arithmetic. More importantly, (column & 07) is not the same
    as (column <= 7). Consider what happens when column is 9, 16,
    23, 24, ...
    , Feb 28, 2006
    #4
  5. david ullua

    david ullua Guest

    Thad Smith 写é“:

    > david ullua wrote:
    > > Hi,
    > > In Expand.c of BSD system, I met the following codes, the expression
    > > (column & 07) if used to compare variable column and 7, if column<=7,
    > > it returns true, else false. It use (column & 07) rather than
    > > (column<=7), thus it brings me a question, does bit operation always
    > > work more efficiently than math operation? How about plus without
    > > carry? and minus without borrow?
    > >
    > > int column = 0;
    > > //other codes .....
    > > do {
    > > putchar(' ');
    > > column++;
    > > } while (column & 07);

    >
    > The bitwise and operator allows the do loop to perform a generalized tab
    > operation, assuming stops every 8 characters. It also works if column >
    > 7, while a simple compare wouldn't. Perhaps in this case, column never
    > exceeds 7 (depending on what "other code"" includes), in which case a
    > compare would be functionally equivalent. C doesn't dictate the
    > relative efficiency, but on most processors both forms would be very
    > simple and efficient.
    >
    > --
    > Thad


    _other_ _codes_ does not modify the value of column.
    david ullua, Feb 28, 2006
    #5
  6. In article <>,
    "david ullua" <> wrote:

    > Hi,
    > In Expand.c of BSD system, I met the following codes, the expression
    > (column & 07) if used to compare variable column and 7, if column<=7,
    > it returns true, else false. It use (column & 07) rather than
    > (column<=7), thus it brings me a question, does bit operation always
    > work more efficiently than math operation? How about plus without
    > carry? and minus without borrow?


    The tests are different.

    if (column & 0x07) ...

    will execute the code following the if-statement when column is 1 to 7,
    9 to 15, 17 to 23, 25 to 31, and so on.

    The basic rule is: Write code in a way that is most readable and most
    clearly expresses what you want to do, and not worry about speed.

    If it turns out that speed is a problem (your software is not acceptable
    because it is too slow), you start _measuring_ the speed. Any attempt to
    improve speed without measuring is futile.

    If you want your code to run fast in general, write readable code and
    write code in the same way as everyone else. That improves the chance
    that the code falls into a pattern that the compiler recognises and can
    produce better code for than normally.
    Christian Bau, Feb 28, 2006
    #6
  7. david ullua

    david ullua Guest

    Christian Bau 写é“:

    > In article <>,
    > "david ullua" <> wrote:
    >
    > > Hi,
    > > In Expand.c of BSD system, I met the following codes, the expression
    > > (column & 07) if used to compare variable column and 7, if column<=7,
    > > it returns true, else false. It use (column & 07) rather than
    > > (column<=7), thus it brings me a question, does bit operation always
    > > work more efficiently than math operation? How about plus without
    > > carry? and minus without borrow?

    >
    > The tests are different.
    >
    > if (column & 0x07) ...
    >
    > will execute the code following the if-statement when column is 1 to 7,
    > 9 to 15, 17 to 23, 25 to 31, and so on.
    >
    > The basic rule is: Write code in a way that is most readable and most
    > clearly expresses what you want to do, and not worry about speed.
    >
    > If it turns out that speed is a problem (your software is not acceptable
    > because it is too slow), you start _measuring_ the speed. Any attempt to
    > improve speed without measuring is futile.
    >
    > If you want your code to run fast in general, write readable code and
    > write code in the same way as everyone else. That improves the chance
    > that the code falls into a pattern that the compiler recognises and can
    > produce better code for than normally.


    Good idea.
    david ullua, Feb 28, 2006
    #7
  8. david ullua

    Robin Haigh Guest

    "david ullua" <> wrote in message
    news:...
    > Hi,
    > In Expand.c of BSD system, I met the following codes, the expression
    > (column & 07) if used to compare variable column and 7, if column<=7,
    > it returns true, else false. It use (column & 07) rather than
    > (column<=7), thus it brings me a question, does bit operation always
    > work more efficiently than math operation? How about plus without
    > carry? and minus without borrow?
    > I have searched through google web and newsgroups, haven't found any
    > relative topics :(
    >
    > int column = 0;
    > //other codes .....
    > do {
    > putchar(' ');
    > column++;
    > } while (column & 07);


    With unsigned numbers, if you want to divide by a power of two, you can
    shift the appropriate number of bits to the right:

    (x >> 3) is the same as (x / 8)

    (3 because 8 == 2^3)

    and if you want the remainder, you look at the bits that would be lost on
    shifting, so

    (x & 7) is the same as (x % 8)

    (7 because 7 == 8-1)

    So testing (x & 7) tests for a non-zero remainder, i.e. non-divisibility by
    8.

    These are common enough idioms, and were more common when compilers were
    more primitive. This is an old program. The programmer probably wasn't
    worrying about speed, he was just doing what he always did from force of
    habit.

    It's unlikely the bit operations will ever be slower, but they may not be
    faster, because compilers can recognise * / % with powers of 2 and generate
    the same code.

    --
    RSH
    Robin Haigh, Feb 28, 2006
    #8
  9. david ullua

    pete Guest

    david ullua wrote:
    >
    > Hi,
    > In Expand.c of BSD system, I met the following codes, the expression
    > (column & 07) if used to compare variable column and 7, if column<=7,
    > it returns true, else false. It use (column & 07) rather than
    > (column<=7), thus it brings me a question, does bit operation always
    > work more efficiently than math operation?


    No.

    > How about plus without
    > carry? and minus without borrow?
    > I have searched through google web and newsgroups, haven't found any
    > relative topics :(
    >
    > int column = 0;
    > //other codes .....
    > do {
    > putchar(' ');
    > column++;
    > } while (column & 07);


    while ((column | ~7U) == 0);

    --
    pete
    pete, Feb 28, 2006
    #9
  10. david ullua

    CBFalconer Guest

    pete wrote:
    > david ullua wrote:
    >

    .... snip ...
    >>
    >> int column = 0;
    >> //other codes .....
    >> do {
    >> putchar(' ');
    >> column++;
    >> } while (column & 07);

    >
    > while ((column | ~7U) == 0);


    Looks like another way of spelling "once". You may as well write:

    } while (0);

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, Feb 28, 2006
    #10
  11. david ullua

    pete Guest

    CBFalconer wrote:
    >
    > pete wrote:
    > > david ullua wrote:
    > >

    > ... snip ...
    > >>
    > >> int column = 0;
    > >> //other codes .....
    > >> do {
    > >> putchar(' ');
    > >> column++;
    > >> } while (column & 07);

    > >
    > > while ((column | ~7U) == 0);

    >
    > Looks like another way of spelling "once".


    while ((column & ~7U) == 0);

    --
    pete
    pete, Mar 1, 2006
    #11
  12. david ullua

    Micah Cowan Guest

    pete <> writes:

    > CBFalconer wrote:
    > >
    > > pete wrote:
    > > > david ullua wrote:
    > > >

    > > ... snip ...
    > > >>
    > > >> int column = 0;
    > > >> //other codes .....
    > > >> do {
    > > >> putchar(' ');
    > > >> column++;
    > > >> } while (column & 07);
    > > >
    > > > while ((column | ~7U) == 0);

    > >
    > > Looks like another way of spelling "once".

    >
    > while ((column & ~7U) == 0);


    Why is this better than:

    while (column & 07);

    which is what OP wrote in the first place?
    Micah Cowan, Mar 1, 2006
    #12
  13. david ullua

    tedu Guest

    Micah Cowan wrote:
    > pete <> writes:
    > > while ((column & ~7U) == 0);

    >
    > Why is this better than:
    >
    > while (column & 07);
    >
    > which is what OP wrote in the first place?


    it's a replacement for (column <= 7); the original is not.
    tedu, Mar 1, 2006
    #13
  14. david ullua

    pete Guest

    tedu wrote:
    >
    > Micah Cowan wrote:
    > > pete <> writes:
    > > > while ((column & ~7U) == 0);

    > >
    > > Why is this better than:
    > >
    > > while (column & 07);
    > >
    > > which is what OP wrote in the first place?

    >
    > it's a replacement for (column <= 7); the original is not.


    If column was unsigned, then it would be a valid replacement.
    It was the best I could do otherwise.

    --
    pete
    pete, Mar 1, 2006
    #14
    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. =?ISO-8859-1?Q?Roland_M=FCller?=
    Replies:
    1
    Views:
    1,627
    =?ISO-8859-1?Q?Roland_M=FCller?=
    Nov 21, 2005
  2. Replies:
    1
    Views:
    329
    Tom Hawtin
    Jan 24, 2007
  3. VK
    Replies:
    15
    Views:
    1,158
    Dr J R Stockton
    May 2, 2010
  4. Steven D'Aprano
    Replies:
    0
    Views:
    90
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    82
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page