#### Archived

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

# Bits and flags

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

## Recommended Posts

The standard method of doing this is to use a variable of size at least n bits, where n is the number of flags you need. For example, to store up to 8 flags I could use a byte variable. You then need to define each flag as the decimal value (or you can use hex, or whatever you like) of each bit in the variable. Eg.

#define FLAG_ONE 1
#define FLAG_TWO 2
#define FLAG_THREE 4
#define FLAG_FOUR 8

and so on for the number of flags you need. You'd also want to call them something more descriptive so you know what they do. Now, to turn a flag on, simply use the bitwise OR operator:

char flags = 0;

flags |= FLAG_ONE; // turns flag one on

You can also enable multiple flags by ORing them together:

flags |= FLAG_TWO | FLAG_THREE | FLAG_FOUR;

To check flag status use the bitwise AND operator:

if(flags & FLAG_THREE) ...

To remove a flag you can either use bitwise XOR, or AND with the one's complement of the flag. The only difference here is that with XOR the flag has to be on to be turned off (if it's already off, it gets toggled back on).

flags ^= FLAG_ONE; // flag one is toggled

flags &= ~FLAG_ONE; // flag is turned off

That's about all there is to it. Good luck!

Regards

Starfall

Thanks a lot!

##### Share on other sites
Is it possible to obtain a value from a series of flags being on\off? Like (flag1=off and flag2=on and flag3=on) equals this number.

##### Share on other sites
Like you suggested yourself, bit shift!

/Niels

##### Share on other sites
Is it possible to obtain a value from a series of flags being on\off? Like (flag1=off and flag2=on and flag3=on) equals this number.

Yes, just bitwise OR the flags together.

n = FLAG_TWO | FLAG_THREE;

Now n holds the value of flag two and flag three turned on. But you should never need to know or use the value of n which is 6, because if you want it you should be using the expression FLAG_TWO | FLAG_THREE for better code readability - and if you change your flags later, it will still work.

I suggest you pick up a good C programming book, this method is explained in depth in most.

Regards

Starfall

[This message has been edited by Starfall (edited November 22, 1999).]

##### Share on other sites
Starfall's got it right...but he left one thing off...you need to COMBINE his and niels post.

IF the flags are in order in the bit field, then you do this

// mask out everything but desired bits
n = flags & (FLAG_A | FLAG_B | FLAG_C);
// shift the number to make it 0 based
n = n >> (OFFSET_OF_LOWEST_FLAG);

please note...you have to KNOW which flag is the low order flag of course...then the offset can be calculated (but it would involve the log base 2 or the lowest field value ... not very efficient). So instead I recommend for each subfield you want to turn into a usefull number you just #define the amount to shift the field right below where you define the other constants....Example:
say you have 8 bit fields....and you think of them as field A, field B, group C,D,E,F, and group G,H. You could say:
#define FLG_A 1
#define FLG_B 2
#define FLG_C 4
#define FLG_D 8
#define FLG_E 16
#define FLG_F 32
#define FLG_G 64
#define FLG_H 128
#define SHIFT_CDEF 2
#define SHIFT_GH 6

now:
// for gh
n = (flags & (FLG_G | FLG_H)) >> SHIFT_GH;

##### Share on other sites
I've got four flags that can be set on and off (or 0 and 1 for my purposes). How can I plug four values into a function that would spit out a unique number for a combination of flags setted.

i.e. the combo 1,0,1,1 would equal a different number than 0,0,1,1.

I've thought of having the first entry bitshifted by 2, the second by 4, the third by 8 and the fourth by 16 but then that would be stupid....

##### Share on other sites
Xai: Why on earth would you want to shift 'n' to the right? By shifting it, it no longer is unique - ie. flag_a | flag_b and flag_b | flag_c both give you the same 'n'.

- Splat

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632985
• Total Posts
3009724
• ### Who's Online (See full list)

There are no registered users currently online

×