位操作总是比数学运算更有效吗? [英] Does bit operation always work more efficiently than math operation?

查看:48
本文介绍了位操作总是比数学运算更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在BSD系统的Expand.c中,我遇到了以下代码,表达式

(列& 07)如果用于比较变量列和7,如果列< = 7,

则返回true,否则返回false。它使用(列& 07)而不是

(列< = 7),因此它给我带来一个问题,位操作总是

比数学运算更有效率?没有

的加号怎么样?没有借用而减去?

我通过谷歌网站和新闻组搜索过,没有找到任何

相关主题:(


int column = 0;

//其他代码.....

do {

putchar('''');

列++;

} while(列& 07);

解决方案

< blockquote> david ullua写道:


在BSD系统的Expand.c中,我遇到了以下代码,表达式
(列& 07)如果用于比较变量列和7,如果列< = 7,则返回true,否则为false。它使用(column& 07)而不是
(列< = 7),因此它给我带来一个问题,位操作总是比数学运算更有效吗?如果没有
进行加号?如果没有借用而减去呢?

int column = 0;
//其他代码.....
执行{
putchar('''');
列++;
} while(列& 07);




按位和运算符允许do循环执行通用选项卡

操作,假设每8个字符停止一次。它也适用于列>

7,而简单的比较则不行。也许在这种情况下,列永远不会超过7(取决于其他代码包含的内容),在这种情况下,

比较在功能上是等效的。 C并没有决定

的相对效率,但在大多数处理器上,这两种形式都非常简单高效。


-

Thad


david ullua写道:


在Expand.c中BSD系统,我遇到了以下代码,表达式
(列& 07)如果用于比较变量列和7,如果列< = 7,
它返回true,否则为false。它使用(列& 07)而不是
(列< = 7),因此它给我带来一个问题,位操作总是比数学运算更有效吗?加上没有
携带怎么样?没有借用而减去?
我搜索了谷歌网站和新闻组,没有找到任何相关主题:(

int column = 0;
/ /其他代码.....
做{
putchar('''');
列++;
} while(列& 07);




这取决于CPU。编译器将C转换为操作码。

CPU有不同的操作码类别。那么什么是更好的一台

机器在另一台机器上更糟糕。或者,完全相同。


编译器优化器的工作是将效果降至最低。


david ullua写道:

在BSD系统的Expand.c中,我遇到了以下代码,表达式
(列& 07)如果用于比较变量列和7,如果列< = 7,
它返回true,否则为false。它使用(column& 07)而不是
(列< = 7),因此它给我带来了一个问题,位操作总是
比数学运算更有效率?加上没有
携带怎么样?没有借用而减去?
我搜索了谷歌网站和新闻组,没有找到任何相关主题:(

int column = 0;
/ /其他代码.....
做{
putchar('''');
列++;
} while(列& 07);




位操作不一定总是比

算术更有效。更重要的是,(column& 07)不一样

as(column< = 7)。考虑当列为9,16时会发生什么,

23,24,...

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


这篇关于位操作总是比数学运算更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆