关于结构的基本问题
我正在检查 Windows 设备驱动程序,并看到如下结构代码:
struct driver1
{
UINT64 Readable : 1;
UINT64 Writable : 1;
UINT64 Executable : 1;
UINT64 Control : 3;
UINT64 Status : 1;
UINT64 Reserved : 51;
UINT64 Available1 : 5;
UINT64 IsMapped : 1;
};
Does every UINT64
indicates a single bit ?冒号代表位吗?
I am going through a windows device driver and I saw struct code like this:
struct driver1
{
UINT64 Readable : 1;
UINT64 Writable : 1;
UINT64 Executable : 1;
UINT64 Control : 3;
UINT64 Status : 1;
UINT64 Reserved : 51;
UINT64 Available1 : 5;
UINT64 IsMapped : 1;
};
Does each UINT64
represent a single bit ? Does a colon represent bits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这意味着
Readable
、Writable
和Executable
各占用一点,Control
占用3,Reserved
51等等。请参阅http://publications.gbdirect.co.uk/c_book/chapter6/bitfields .html 了解更多信息。
UINT64 只是意味着整个位域将位于 64 位无符号整数内。
This means that
Readable
,Writable
andExecutable
each take up a bit,Control
takes 3,Reserved
51 and so on.Refer to http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html for more info.
The UINT64 simply means that the entire bitfield will be inside a 64 bit unsigned integer.
就是这个想法,是的。它称为位域。该数字表示编码器要求该字段占用的位数。如果你把它们全部数起来,你会发现它们加起来是 64。
问题是 C++(与 Ada 不同)没有提供真正的方法来保证整个结构只占用 64 位。因此,如果您在其设计运行的系统之外的系统上编译它,我会检查一下以确保确定。
当我用 C++ 编写设备驱动程序时,我不使用位域。我改用位掩码。当然,问题在于您必须了解您的平台如何排序其字节。
That is the idea, yes. Its called a bitfield. The number indicates the number of bits the coder is asking for that field to take up. If you count them all up, you'll see that they add up to 64.
The problem is that C++ (unlike Ada) provides no real way to guarantee that the whole struct only takes up 64 bits. So if you are compiling this on a system other than the one it was designed to run on, I'd check it out to be sure.
When I write device drivers in C++, I don't use bitfields. I use bitmasks instead. The problem there of course is that you have to be aware of how your platform orders its bytes.
这些是C 中的位域,因此您可以通过结构独立访问这些位。
These are bitfields in C, so you can access those bits independently via the struct.