比特移位与比特场 [英] Bit shifting vs Bitfields

查看:85
本文介绍了比特移位与比特场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我正在编写一个接收单词的应用程序,这是一个位图。我
创建了一个word typedef,其中包含一个定义每个

位的位域。然而,我想知道是否更好地写一个

宏来访问每个位而不是位域。


我读过C-关于位字段的常见问题,但是想知道是否有使用位移的任何优点/缺点

。在我看来,我认为使用位域的
更合乎逻辑,更易于阅读和使用,但感觉它比位移更慢。

有谁能开导我这个问题?


问候,


Richard

Hello All,

I am writing an application which receives a word which is a bitmap. I
have created a word typedef which contains a bitfield defining each
bit. however, I was wondering however if it would be better to write a
macro to access each bit instead of the bitfield.

I have read the C-FAQ on bit fields, but was wondering if there were
any advantages/disadvantages to using bit shifting. To my mind I think
using bitfields are more logical and easier to read and use but have a
feeling it is slower than bit shifting.

Can anyone enlighten me on this subject?

Regards,

Richard

推荐答案

ri ******* @ later .demon.co.uk 写道:
Hello All,

我正在编写一个接收一个位图的应用程序。我创建了一个word typedef,其中包含一个定义每个
位的位域。然而,我想知道是否更好地编写一个
宏来访问每个位而不是位域。

我已经阅读了关于位域的C-FAQ,但是想知道如果使用位移有任何优点/缺点。在我看来,我认为使用位域更合乎逻辑,更容易阅读和使用,但感觉它比位移更慢。
Hello All,

I am writing an application which receives a word which is a bitmap. I
have created a word typedef which contains a bitfield defining each
bit. however, I was wondering however if it would be better to write a
macro to access each bit instead of the bitfield.

I have read the C-FAQ on bit fields, but was wondering if there were
any advantages/disadvantages to using bit shifting. To my mind I think
using bitfields are more logical and easier to read and use but have a
feeling it is slower than bit shifting.



Don'' t推测,个人资料!


由于你提到的原因以及每当我看到b

'非常严格的代码来访问它们。


只要确保你不会遇到任何endian问题。


-

Ian Collins。


Don''t speculate, profile!

I''m a bit field fan for the reasons you mention and whenever I''ve
bothered to look, the compiler generated very tight code to access them.

Just make sure you don''t run into any endian issues.

--
Ian Collins.


2006年6月13日01:38:47 -0700, ri ******* @ later.demon.co.uk

comp.lang.c中写道:
On 13 Jun 2006 01:38:47 -0700, ri*******@latter.demon.co.uk wrote in
comp.lang.c:
Hello All,

我正在编写一个接收单词的应用程序,这是一个位图。我创建了一个word typedef,其中包含一个定义每个
位的位域。但是,我想知道是否更好地编写一个
宏来访问每个位而不是位域。


也许它会,也许不会。

我已经阅读了关于位域的C-FAQ,但是想知道是否有任何优势/使用位移的缺点。在我看来,我认为使用位域更合乎逻辑,更容易阅读和使用,但感觉它比位移更慢。


为什么你关心哪种方法更快或更慢?在下面的程序返回TRUE之前你根本不应该关心




enum {FALSE,TRUE} BOOL;

BOOL should_I_worry_about_speed(void)

{

BOOL result = FALSE;


if(this_part_of_my_program_is_tested_and_verified_co rrect()

&& my_program_is_finished_and_works_correctly()

&& my_correct_program_runs_too_slowly()

&& I_have_verified_this_code_is_a_bottleneck())

/ *然后只有* /

{

结果= TRUE;

}


返回结果;

}


当然,您需要实现所调用的四个函数。或者甚至更好地将它们视为伪代码,只需回答问题

。如果且仅当所有四个问题的答案都是真实的,那么你就可以开始思考和测试比特字段是否比比特掩码更快b / b
反之亦然。

有谁能开导我这个问题?
Hello All,

I am writing an application which receives a word which is a bitmap. I
have created a word typedef which contains a bitfield defining each
bit. however, I was wondering however if it would be better to write a
macro to access each bit instead of the bitfield.
Maybe it would, maybe not.
I have read the C-FAQ on bit fields, but was wondering if there were
any advantages/disadvantages to using bit shifting. To my mind I think
using bitfields are more logical and easier to read and use but have a
feeling it is slower than bit shifting.
Why do you care which method is faster or slower? You shouldn''t care
at all until the following program returns TRUE:

enum { FALSE, TRUE } BOOL;

BOOL should_I_worry_about_speed(void)
{
BOOL result = FALSE;

if (this_part_of_my_program_is_tested_and_verified_co rrect()
&& my_program_is_finished_and_works_correctly()
&& my_correct_program_runs_too_slowly()
&& I_have_verified_this_code_is_a_bottleneck())
/* then and only then */
{
result = TRUE;
}

return result;
}

Of course, you need to implement the four functions called. Or even
better, treat this as pseudo code and just answer the questions
yourself. If, and only if, the answers to all four questions are
true, then you can start thinking, and testing, whether bit fields are
faster than bit masks, or vice-versa.
Can anyone enlighten me on this subject?




即使以上所有答案都是真的,唯一的方法就是知道是否

位字段或位掩码在您的特定编译器上更快,并且
处理器是以两种方式和时间构建测试代码。


我见过具有机器语言指令的处理器,它们支持位字段操作,以及使用它们的C编译器,

结果是使用位字段的代码使用

位掩码执行比代码更快。我已经看到了相反的平台。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http:// www。 parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc .html



Even if all the above answers are true, the only way to know whether
bit fields or bit masks are faster on your particular compiler and
processor is to build test code both ways and time it.

I have seen processors that have machine language instructions that
support bit-field manipulation, and C compilers that use them, with
the result that code using bit fields executes faster than code using
bit masks. And I have seen platforms where the opposite is true.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


" Jack Klein" < JA ******* @ spamcop.net>写了
"Jack Klein" <ja*******@spamcop.net> wrote
我已经阅读了关于位域的C-FAQ,但是想知道使用位移是否有任何优点/缺点。在我看来,我认为使用位域更合乎逻辑,更容易阅读和使用,但感觉它比位移更慢。
I have read the C-FAQ on bit fields, but was wondering if there were
any advantages/disadvantages to using bit shifting. To my mind I think
using bitfields are more logical and easier to read and use but have a
feeling it is slower than bit shifting.



为什么你关心哪种方法更快或更慢?在下面的程序返回TRUE之前你根本不应该关心

枚举{FAL,TRUE} BOOL;

BOOL should_I_worry_about_speed(void)
{
BOOL结果= FALSE;

if(this_part_of_my_program_is_tested_and_verified_co rrect()
&& my_program_is_finished_and_works_correctly()
&& my_correct_program_runs_too_slowly()
&& I_have_verified_this_code_is_a_bottleneck())
/ *然后只有* /
{
结果= TRUE;
}

返回结果;
}

当然,您需要实现所调用的四个函数。或者甚至更好,将其视为伪代码并自己回答问题。如果且仅当所有四个问题的答案都是真的,那么你可以开始思考和测试比特字段是否比比特掩码更快,反之亦然。



Why do you care which method is faster or slower? You shouldn''t care
at all until the following program returns TRUE:

enum { FALSE, TRUE } BOOL;

BOOL should_I_worry_about_speed(void)
{
BOOL result = FALSE;

if (this_part_of_my_program_is_tested_and_verified_co rrect()
&& my_program_is_finished_and_works_correctly()
&& my_correct_program_runs_too_slowly()
&& I_have_verified_this_code_is_a_bottleneck())
/* then and only then */
{
result = TRUE;
}

return result;
}

Of course, you need to implement the four functions called. Or even
better, treat this as pseudo code and just answer the questions
yourself. If, and only if, the answers to all four questions are
true, then you can start thinking, and testing, whether bit fields are
faster than bit masks, or vice-versa.

任何人都可以在这个问题上给我启发吗?
Can anyone enlighten me on this subject?



即使以上所有答案都是真的,唯一的方法就是知道是否有位字段或特殊编译器上的位掩码速度更快,而处理器则是在两种方式和时间内构建测试代码。

我见过具有机器语言指令的处理器,它们支持位 - 字段操作和使用它们的C编译器,结果是使用位字段的代码比使用位掩码的代码执行得更快。我已经看到了相反的平台。



Even if all the above answers are true, the only way to know whether
bit fields or bit masks are faster on your particular compiler and
processor is to build test code both ways and time it.

I have seen processors that have machine language instructions that
support bit-field manipulation, and C compilers that use them, with
the result that code using bit fields executes faster than code using
bit masks. And I have seen platforms where the opposite is true.



Bitfields曾经普遍被严重执行。

有一种恶性循环。程序员使用掩码是因为

位域代码很慢,并且编译器编写者没有太多优点来优化

位域,因为它们很少被使用。


我确定是否仍然如此。现在内存大小太大了,通常尝试将数据压缩到

最小位数是没有多大意义的。

-

买我的书12普通无神论者论点(驳斥)


Bitfields were once generally badly implemented.
There was a kind of vicious circle. Programmers used masks because the
bitfield code was slow, and complier writers didn''t bother much to optimise
bitfields because they were rarely used.

I am sure whether this is still the case. Now that memory sizes are so
large, usually it doesn''t make much sense to try to squash data into a
minimal number of bits.
--
Buy my book 12 Common Atheist Arguments (refuted)


这篇关于比特移位与比特场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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