如何使用长于64位的位字段? [英] How to work with bitfields longer than 64 bits?
问题描述
问题说的一切。
如果我有一个96位字段:
If I have this for a 96-bit field:
uint32_t flags[3]; //(thanks @jalf!)
我最好去访问这个,因为我的子字段可以位于32位边界(例如,从位29到35的字段)上。
How do I best go about accessing this, given that my subfields therein may lie over the 32-bit boundaries (eg. a field that runs from bit 29 to 35)?
我需要我的访问尽可能快,我不想将它们迭代为数组的32位元素。
I need my accesses to be as fast as possible, so I'd rather not iterate over them as 32-bit elements of an array.
推荐答案
]
[This answer is valid for C (and by extension, for C++ as well).]
平台无关的方式是适当地应用位掩码和位移位
The platform-independent way is to apply bit-masks and bit-shifts as appropriate.
因此,您的字段从29到35(含):
So to get your field from 29 to 35 (inclusive):
(flags[1] & 0xF) << 3
| (flags[0] & 0xE0000000) >> 29 // The bitmask here isn't really needed, but I like symmetry!
显然,你可以编写一个类/函数/宏来自动化。
Obviously, you could write a class/function/macro to automate this.
这篇关于如何使用长于64位的位字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!