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); 13 2292
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
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.
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, ...
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.
In article <11**********************@t39g2000cwt.googlegroups .com>,
"david ullua" <da*********@gmail.com> 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 写道: In article <11**********************@t39g2000cwt.googlegroups .com>, "david ullua" <da*********@gmail.com> 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" <da*********@gmail.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com... 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
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 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 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 <pf*****@mindspring.com> 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 wrote: pete <pf*****@mindspring.com> 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 wrote: Micah Cowan wrote: pete <pf*****@mindspring.com> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Greg Kuperberg |
last post by:
I plan to use pydoc for my Python project. After looking through the
standard documentation, I am not sure how pydoc interprets its input.
In its basic operation it evidently looks at the first...
|
by: |
last post by:
If I need to check if a certain value does exist in a field, and return
either "yes" or "not" which query would be the most effestive?
|
by: Fredrik Celin |
last post by:
I use IE 6.0 and does some calculations in a javascript.
If I run this code:
alert(1.4 - 0.5);
i get the result 0.8999999999 and not 0.9
Does anybody know why? Is this a bug in IE or...
|
by: Larry David |
last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service"....
What does "64bit" mean to your friendly...
|
by: Homer Simpson |
last post by:
Hi everyone,
I wrote a quick method to perform a calculation to determine the average of
two numbers. When 23 and 40 were used, the result was 31, not 31.5! I
discovered the problem, I was using...
|
by: Claudio Grondi |
last post by:
Let's consider a test source code given at the very end of this posting.
The question is if Python allows somehow access to the bytes of the
representation of a long integer or integer in...
|
by: webEater |
last post by:
I have a problem, it's not browser specific, and I don't get a
solution. I have an (X)HTML document, I show you a part of it:
....
<!--<div class="pad">-->
<div id="eventImages"><img src=""...
|
by: kidko |
last post by:
I'm just trying to move a few images around a page... The images already exist, and the following function simply assigns them an ID and sets their offsetTop/offsetLeft attributes. This works for...
|
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= |
last post by:
Hi,
Why does Math.Sqrt() only accept a double as a parameter? I would think
it would be just as happy with a decimal (or int, or float, or ....). I can
easily convert back and forth, but I am...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |