Jump to content
  • Advertisement
Sign in to follow this  
Tispe

unfamiliar variable declaration in struct

This topic is 2357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

I am reading some old c++ code, from about 2003 and I noticed in a struct declaration is a colon followed by a number after the variable:


typedef struct
{
DWORD id;
long type:7;
long next:19;

.
.
.


what does it do?

Share this post


Link to post
Share on other sites
Advertisement
It's called 'bit fields', and means that 'type' uses 7 bits and 'next' uses 19 bits. Google gives for example this with some examples: http://www.cs.cf.ac.uk/Dave/C/node13.html

(You should be aware bit ordering isn't guaranteed and that structures can be padded, so don't count on it giving exactly any special bit representation of the struct unless you analyze it for a specific platform and compiler).

Share this post


Link to post
Share on other sites
So I am guessing it is just a way of saving a few bits of memory if you know that the variable will not exceed a certain value (4 bits are 0-15). I am guessing you can only write bytes to files, which means that the struct is padded to the next whole byte.

Also, cutting 7 bits out of a struct is pointless since it has to be padded up 7 bits again, right?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!