开发者

Basic Question regarding structs

开发者 https://www.devze.com 2023-02-12 11:43 出处:网络
I am going through a windows device driver and I saw struct code like this: struct driver1 { UINT64 Readable: 1;

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开发者_如何学Python Reserved     : 51; 
       UINT64 Available1   : 5; 
       UINT64 IsMapped     : 1;
};

Does each UINT64 represent a single bit ? Does a colon represent bits?


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消