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~
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
Hm.... for the ''bitfield'', most people use DWORD or WORD(unsigned long or unsigned short).
And for the constant (for a DWORD):
0x00000001
0x00000002
0x00000004
0x00000008
0x00000010
0x00000020
0x00000040
0x00000080
0x00000100
...
0x80000000 (i think this is the max)
And for the constant (for a DWORD):
0x00000001
0x00000002
0x00000004
0x00000008
0x00000010
0x00000020
0x00000040
0x00000080
0x00000100
...
0x80000000 (i think this is the max)
"after many years of singularity, i'm still searching on the event horizon"
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.
Then to set Personality with any one of these properties (to true), just do this:
To set a certain bit to 0, do this:
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:
There you go! I think that should cover everything. Good luck.
ColdfireV
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.
ColdfireV
[email=jperegrine@customcall.com]ColdfireV[/email]
And if you know that the person isn't nice:
Personality &=~Nice;
Visit our homepage: www.rarebyte.de.st
GA
Edited by - ga on 4/23/00 1:23:17 PM
Personality &=~Nice;
Visit our homepage: www.rarebyte.de.st
GA
Edited by - ga on 4/23/00 1:23:17 PM
Visit our homepage: www.rarebyte.de.stGA
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;
thisone->Post("www.gamedev.net");
}
:-)
HAPPY EASTER ALL! (if your into that stuff hehe)
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;
thisone->Post("www.gamedev.net");
}
:-)
HAPPY EASTER ALL! (if your into that stuff hehe)
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
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
mlbobs@telocity.com
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
mlbobs@telocity.com
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:
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**
If you look closely you''ll see that you''ve in fact got these constants:
0x0001 << 10x0001 << 20x0001 << 30x0001 << 4etc..
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**
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
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.
(BV stands for bitvector in my code, btw.)
Example of use:
More commonly though, I will use predetermined constants for other things. So I would declare them like this:
And, of course, use this like:
Hope that helps.
Edited by - Kylotan on 4/24/00 7:57:44 AM
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{ CONSTANT1 = BV00, CONSTANT2 = BV01};
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
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? :-)
var = 1 << 1;
var = 1 << 23;
var = 1 << 35;
Or am I lost? :-)
----------------------------noxa - Ben Vanikhttp://24.3.123.4----------------------------
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement