本文介绍了当它是值得使用位字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C的位域实现是否值得?如果是这样,当它使用过?

Is it worthwhile using C's bit-field implementation? If so, when is it ever used?

我一直在寻找通过一些模拟器code,它看起来像使用位字段没有被实施的寄存器芯片。

I was looking through some emulator code and it looks like the registers for the chips are not being implemented using bit fields.

这是东西,避免由于性能原因(或其他原因)?

Is this something that is avoided for performance reasons (or some other reason)?

是否还有时间,当使用位字段? (即固件将实际芯片等)

Are there still times when bit-fields are used? (ie firmware to put on actual chips, etc)

推荐答案

位字段通常只用于。一个例子可能是组装一个IP数据包报头。我看不出一个令人信服的理由,一个模拟器使用位字段来模拟一个寄存器,因为它永远不会触及真正的硬件!

Bit-fields are typically only used when there's a need to map structure fields to specific bit slices, where some hardware will be interpreting the raw bits. An example might be assembling an IP packet header. I can't see a compelling reason for an emulator to model a register using bit-fields, as it's never going to touch real hardware!

虽然位字段可导致整齐的语法,它们是pretty,因此不可移植的平台依赖性。更便携,但还没有更详细的,方法是使用直接位操作,通过移位和位掩码。

Whilst bit-fields can lead to neat syntax, they're pretty platform-dependent, and therefore non-portable. A more portable, but yet more verbose, approach is to use direct bitwise manipulation, using shifts and bit-masks.

如果您使用位字段比组装(或拆卸)结构的一些物理接口等什么,性能可能会受到影响。这是因为每次你读或从位字段写入时,编译器将产生code做掩蔽和转移,这会燃烧循环。

If you use bit-fields for anything other than assembling (or disassembling) structures at some physical interface, performance may suffer. This is because every time you read or write from a bit-field, the compiler will have to generate code to do the masking and shifting, which will burn cycles.

这篇关于当它是值得使用位字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:41