如何与位域超过64位不再起作用? [英] How to work with bitfields longer than 64 bits?

查看:324
本文介绍了如何与位域超过64位不再起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明了一切。

如果我有这样的一个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.

推荐答案

[这个答案有效期为C(通过扩展,对C ++以及)。]

与平台无关的方法是采用位掩码和位转变为适当的。

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屋!

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