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 careat 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 evenbetter, treat this as pseudo code and just answer the questionsyourself. If, and only if, the answers to all four questions aretrue, then you can start thinking, and testing, whether bit fields arefaster 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 .htmlEven if all the above answers are true, the only way to know whetherbit fields or bit masks are faster on your particular compiler andprocessor is to build test code both ways and time it.I have seen processors that have machine language instructions thatsupport bit-field manipulation, and C compilers that use them, withthe result that code using bit fields executes faster than code usingbit masks. And I have seen platforms where the opposite is true.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.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 thebitfield code was slow, and complier writers didn''t bother much to optimisebitfields because they were rarely used.I am sure whether this is still the case. Now that memory sizes are solarge, usually it doesn''t make much sense to try to squash data into aminimal number of bits.--Buy my book 12 Common Atheist Arguments (refuted) 这篇关于比特移位与比特场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 17:40