If he's using a 4-byte integer (32) bits to make a Bit mask it's quite simple to add things. Say for example I have one byte:0000
The "furthest left" bit is referred to as the "Most Significant Bit" (as it represents the highest value, in this case
, the "furthest right" bit is called the "least significant bit" (it represents 1).
Lets say in your example that the MSB represents a randomized head and the bit to the right of it represents the randomized hair color.
Using boolean math you can make a bitmask out of this like follows. Say for example I have the following consts
const int RANDOM_HEAD = 8
const int RANDOM_HAIIR_COLOR = 4
const int RANDOM_.... = 2
const int RANDOM_... = 1
If you want to set the flag to include a random head and a random color do the following (lets say you're passing it into the function
void Foo( int MY_BIT_MASK ) {
}
you can make a function call like this:
Foo( (RANDOM_HEAD|RANDOM_HAIR_COLOR) );
(note the use of PIPE ( | ) to OR them together)
This will cause the language to BITWISE OR the value together.
if you want to TEST for what flags are set, it simple, you can do so using BITWISE AND'ing to check for a
certain flag
for example
void Foo( int MY_BIT_MASK ) {
if( MY_BIT_MASK & RANDOM_HEAD ) {
//code to run for random head here
}
if( MY_BIT_MASK & RANDOM_HAIR_COLOR ) {
//code to run for random hair color
}
}
Using this method, you can store a maximum of 32 flags inside a single 4-byte integer.
Hope this helps.
Modifié par Tiggers.no.tail, 13 juillet 2011 - 04:35 .