Jump to content
  • Advertisement
Sign in to follow this  
choffstein

Bit Fields

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

I just recently re-discovered bit fields. For those of you who do not know what bit fields are, here is a small explaination: a bit can have 2 states, (0 or 1). The idea is that for something that has states, to use bit fields to save space. Why use an integer or a char for something that has only two states, when all you really need is a bit? Simply use it like such: unsigned int name:1; That would simply create a bit in memory. name would only be able to be assigned the values 0 or 1, because those are the only values that can be created using 1 bit. If we had done: unsigned int name:4; Then we would have been able to assign anywhere between 0 and 15. So my question is, besides the fact that bit fields often make code a little dirty, why do I see them so rarely? I suppose #defines could make code legible, and I would think that with their space saving ability, they would be more widely used...especially in network programming. Anyone use these buggers? Any thoughts on effective usage? -visage

Share this post


Link to post
Share on other sites
Advertisement
I'd say that's because unless you're talking to hardware or are direly constrained in memory, there is little need to bother with them. Plus, they're not magic: it all compiles down to ands, ors and bitshifts anyway.

Finally, many people don't even know they exist... chalk that one up to education.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Plus, they're not magic: it all compiles down to ands, ors and bitshifts anyway.


Well, I understand that, but if a char is 8 bits, and you only need 1, that seems to be a lot of space you are saving -- especially with network data and message based systems.

Maybe they make more sense on consoles and mobile units, where memory is not nearly as abundant as it is on PCs.

Share this post


Link to post
Share on other sites
Just coded some tonight; and now I read this post.. heh , the coincedence.
I use them to store a range of Boolean values; 1 byte can store 8 values.
Very handy. I use bytes and bits all the time to store any info.

Share this post


Link to post
Share on other sites
Now, a downside is that you cannot take a pointer or a reference to a bitfield member. Which is why std::vector<bool> is so broken.

struct foo
{
char a : 1;
char b : 1;
char c : 6;
};

int main()
{
int foo::*ptr = &foo::a;
}

foo.cc: In function 'int main()':
foo.cc:10: error: invalid pointer to bit-field 'foo::a'

Share this post


Link to post
Share on other sites
Quote:
Original post by visage
Well, I understand that, but if a char is 8 bits, and you only need 1, that seems to be a lot of space you are saving -- especially with network data and message based systems.

Nope. If you just declare a single bit, it'll still allocate a byte. Which means you're using just as much memory.

I used to use them when I needed multiple flags, but I've since stopped. I don't really have a reason.

CM

Share this post


Link to post
Share on other sites
Conner, you are such a stickler, though you are right. Bit fields are only effective when used several at a time, or you are just wasting space. Though, I still see the ability to have essentially 8 bools packed into 1 byte fairly useful...

Fruny, why is that?

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Finally, many people don't even know they exist... chalk that one up to education.


Bingo! I know I'd never seen them until I was reading a Real Time Strategy Game Programming book. I was like what the heck are the colons doing [lol]. However though, education fails to teach a lot of things about C++...

Share this post


Link to post
Share on other sites
A lot of space can be saved this way if we're talking about network applications, and when we're talking about cutting down on traffic. But if we're talking about an word processor, a browser, image editor, games, it really isn't worth it because of the insignificant amount of space we're talking about compared with today's memory capacity and the memory alignment issue, that will slow down the write and read operations on those values.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!