关于结构的基本问题

发布于 2024-10-18 20:19:47 字数 420 浏览 0 评论 0原文

我正在检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

西瑶 2024-10-25 20:19:47

这意味着ReadableWritableExecutable各占用一点,Control占用3,Reserved51等等。

请参阅http://publications.gbdirect.co.uk/c_book/chapter6/bitfields .html 了解更多信息。

UINT64 只是意味着整个位域将位于 64 位无符号整数内。

This means that Readable, Writable and Executable 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.

无可置疑 2024-10-25 20:19:47

就是这个想法,是的。它称为位域。该数字表示编码器要求该字段占用的位数。如果你把它们全部数起来,你会发现它们加起来是 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.

深海不蓝 2024-10-25 20:19:47

这些是C 中的位域,因此您可以通过结构独立访问这些位。

These are bitfields in C, so you can access those bits independently via the struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文