• Advertisement

Archived

This topic is now archived and is closed to further replies.

Bit flags

This topic is 5101 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

Suppose you needed a bunch of bit flags for something that went well over the size of an integer or something, and for memory concerns wanted to store them using bits instead of the normal byte bools. consider struct flags { bool flag:1; }; This would allocate 1 bit for the flag variable right? Now, would the compiler pad the structure with extra stuff for alignment, therefor defeating the purpose of trying to use 1 bit, or would the struct be 1 bit. I want to be able to track a large number of flags, using 1 bit, and would need the ability to add them as needed. I''m pondering something like enum flagEnum { flag1, flag2, flag3, flag4, max_flags, }; and then later doing struct flags[max_flags]; And I''m wondering if this method would do the flags at the bit level, or if padding or some other reason would be a problem.

Share this post


Link to post
Share on other sites
Advertisement
This wouldn''t work, the struct will be at least 1 byte (probably 4). If you have multiple flags like this you need to do something like this:

struct flags_t
bool flag1 : 1;
bool flag2 : 1;
bool flag3 : 1;
bool flag4 : 1;
} flags;

Share this post


Link to post
Share on other sites
You might want to check out std::bitset if you''re using C++.

Share this post


Link to post
Share on other sites

  • Advertisement