Basic Question regarding structs

白昼怎懂夜的黑 提交于 2019-12-13 17:44:07

问题


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?


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/5082256/basic-question-regarding-structs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!