473,698 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does bit operation always work more efficiently than math operation?

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);

Feb 28 '06 #1
13 2342
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
Feb 28 '06 #2
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.
Feb 28 '06 #3
jjf
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 '06 #4

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.

Feb 28 '06 #5
In article <11************ **********@t39g 2000cwt.googleg roups.com>,
"david ullua" <da*********@gm ail.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.
Feb 28 '06 #6

Christian Bau 写道:
In article <11************ **********@t39g 2000cwt.googleg roups.com>,
"david ullua" <da*********@gm ail.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.

Feb 28 '06 #7

"david ullua" <da*********@gm ail.com> wrote in message
news:11******** **************@ t39g2000cwt.goo glegroups.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
Feb 28 '06 #8
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
Feb 28 '06 #9
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.c om, 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/googlegroupsrep ly/>

Mar 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3111
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 string literal in the module and in each function definition. But there is more to the story than that, obviously. What other vestigial code does it detect? Every string literal? Variables of the form __xxx__, I gather? Which of these variables...
20
10149
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?
10
2903
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 what? Any fast and easy ways to work around this problem?
58
30221
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 neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
2
1319
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 long data types, not double. So, how does c# and/or .NET round numbers? Does is just truncate the value? If rounded, I think 31.5 should have been rounded to 32. Thanks, Scott
15
2346
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 computers memory? Or does Python hide such implementation details that deep, that there is no way to get down to them? The test code below shows, that extracting bits from an integer value n is faster when using n&0x01 than when using n%2 and I...
14
3485
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="" id="eventImage0" class="eventImage"><img src="" id="eventImage1" class="eventImage"></div>
5
1958
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 other images, but these refuse to cooperate. The alert halfway in is an attempt to find the problem, but it always returns the right values. Once the function finishes, however, they're always totally off, and just line up horizontally at the top-left...
13
10524
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 interested in what is going on behind the scenes and if there is some aspect of decimals that keep them from being used in this calculation. Thanks! Ethan Ethan Strauss Ph.D.
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.