Jump to content
  • Advertisement


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



This topic is 6940 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''ve been wondering this for some time: how do you properly use bitfields? For my entire coding ''career'' I''ve been using bool''s to check for a situation... This is a horrible way to do it, it clutters my structs/classes, takes up more memory, and is a pain to program with. My question is, how do I implement bitfields correctly? I have several sources that contain bits of code, but none of them explain it. So far, heres what I know: -Constants must be different so that none equal the same bit -(var) & CONSTANT checks for CONSTANT -(var) = var / CONSTANT adds CONSTANT not enought to implement. What I need to know are the values for CONSTANT. Some I know are 0x01 0x02 0x04 0x08 but then the code that I''m using to go off of skips around (or at least I think it does), going from 0x08 to 0x32, skipping 0x16, which I thought would be the next one. Also, what is the max for these values? Thanks for any help on this topic, I''m sure many know how to do it (but I don''t, boo hoo :-( ~noxa~

Share this post

Link to post
Share on other sites
Hm.... for the ''bitfield'', most people use DWORD or WORD(unsigned long or unsigned short).

And for the constant (for a DWORD):
0x80000000 (i think this is the max)

Share this post

Link to post
Share on other sites
Yes, each of those constants in the last post are the values to use for a DWORD. Here''s an example.

I''m just going to use an unsigned char, because I don''t need that many bits to hold my bools.

const int Nice = 0x01,
Friendly = 0x02,
UserFriendly = 0x04,
Shy = 0x08,
Stupid = 0x10,
Mean = 0x20,
Lame = 0x40,
Funny = 0x80;
unsigned char Personality = 0;

Then to set Personality with any one of these properties (to true), just do this:

Personality /= Funny;
Personality /= Nice;

To set a certain bit to 0, do this:

Personality ^= Funny;
Personality ^= Nice;

Actually, that will set the bit to whatever it was not set to before. It will change TRUE to FALSE and FALSE to TRUE.

And lastly, to check whether or not a certain bit is true, do this:

if (Personality & Funny)
//Do something funny
if (Personality & Nice)
//Do something nice

There you go! I think that should cover everything. Good luck.


Share this post

Link to post
Share on other sites
hehe, thanks all!
Would this code be correct?

#define POST_SIMPLE 0x00000001
#define POST_FUNNYREPLYS 0x00000002
#define POST_HELPFULLPEOPLE 0x00000004

void main()
CPost thisone;
thisone->g_dwFlags /= POST_SIMPLE;
thisone->g_dwFlags /= POST_FUNNYREPLYS;
thisone->g_dwFlags /= POST_HELPFULLPEOPLE;


HAPPY EASTER ALL! (if your into that stuff hehe)

Share this post

Link to post
Share on other sites
I personally go for real bit fields in a structure. Code comes out a little less cluttered. There are some minor portability issues: any up todate compiler should handle the bitfields, but some older ones don''t and if you design assuming the byte count you might get bit (heh heh) on a port sometimes (when int changes from 4 to 8 or 2, as an example), but then you can design for that, too.

I guess some compilers might do one or the other more efficiently, but a little testing I once did turned up no difference at all at the assembler level from Visual C++ 6.0.

Mike Roberts
aka milo

Share this post

Link to post
Share on other sites
An easier way to do the constant-allocation thing is by bit-shifting.
If you look closely you''ll see that you''ve in fact got these constants:

0x0001 << 1
0x0001 << 2
0x0001 << 3
0x0001 << 4

That may look horrible, but it''s actually what you are trying to achieve . Makes for less errors too;µ

#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post

Link to post
Share on other sites
To go one step further than MadKeithV... I use this following method to reduce the chance of typing errors. Any time you use an integral constant, consider using a const int or an enum, if only so that the compiler can catch any typos you make. A side-effect is that you have more readable and quickly-modifiable code.

enum Int32Bits
BV00 = 1 << 0,
BV01 = 1 << 1,
BV02 = 1 << 2,
BV31 = 1 << 31

(BV stands for bitvector in my code, btw.)

Example of use:
if (var & BV03) /* do something */; 

More commonly though, I will use predetermined constants for other things. So I would declare them like this:

enum Flags

And, of course, use this like:
if (var & CONSTANT1) /* do something */; 

Hope that helps.

Edited by - Kylotan on 4/24/00 7:57:44 AM

Share this post

Link to post
Share on other sites
hmmm, wait, so with that method, you can use all numbers? like

var = 1 << 1;
var = 1 << 23;
var = 1 << 35;

Or am I lost? :-)

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!