Let’s say that we have some values, represented by power of 2:
TYPE_1 = 1
TYPE_2 = 2
TYPE_3 = 4
TYPE_4 = 8
...
I need to store some of these types in one value.
Example:
To represent TYPE_1
with TYPE_3
, the value is 5
.
What operation is used to identify that value 5
represent TYPE_1
with TYPE_3
?
2
What operation is used to identify that value 5 represent TYPE_1 with
TYPE_3?
I’m going to assume you mean that you’ve been handed a value
(say, 5
) and want to know how to tell that the first and third bits are set and therefore represent TYPE_1
and TYPE_3
.
There are a couple of ways to handle this. Both involve using a series of logical ANDs to shut off all of the bits in the value you’ve been handed except the one you’re interested in to see if the remaining bit is 1
or 0
:
00000101 The value; 5 in this case
and 00000100 The mask, TYPE_3
--------
00000100 The ANDed result, which is nonzero. TYPE_3's bit is set.
Same thing, but with TYPE_2
, which isn’t present in the value
:
00000101 The value; 5 in this case
and 00000010 The mask, TYPE_2
--------
00000000 The ANDed result, which is zero. TYPE_2's bit is not set.
The first way to handle this is to do comparisons against each type:
if ( value & TYPE_1 ) handle_type_1();
if ( value & TYPE_2 ) handle_type_2();
...etc...
This is the brute force method, but if you’re dependent on the TYPE_*
macros defined in some header file, this is the only way to do it safely. You can’t depend on the bits representing each type not changing. (And shame on whoever wrote the header file for not providing some functions or macros to help you figure these things out.)
If you do have control over the arrangement of the bit field and can declare that bit n
represents type n
, it becomes easy to put together a few macros (or functions if you’re not using C) to handle it:
#define NUMBER_OF_TYPES 4
/* These are bit numbers, not powers of two */
#define TYPE_1 1
#define TYPE_2 2
#define TYPE_3 3
#define TYPE_4 4
#define TYPE_MASK(t) (1 << (t-1))
#define VALUE_HAS_TYPE(v,t) ((v) & TYPE_MASK((t)))
/* Construct a value holding two types */
unsigned value = TYPE_MASK(TYPE_1) | TYPE_MASK(TYPE_3);
for (unsigned type_num=0 ; type_num < NUMBER_OF_TYPES ; ++type_num) {
if ( VALUE_HAS_TYPE(value, type_num)
do_something_with_type_number(type_num);
}
This iterates over the number of types you have, checks to see if each one is present and then calls a function with its type number. This gives you a huge amount of flexibility in do_something_with_type_number()
, because you can use a switch
statement on the type number or use it as an index into an array that might point at functions to handle each type. You can’t do either of those with bitwise comparisons.
If you use the bitwise or operator, you will get the value 5
print(TYPE_1 | TYPE_3) //5
2
I’m using C here, but similar operators exist in most languages.
Assume you have some variable to store the types:
unsigned char mytypes;
#define TYPE_1 1
#define TYPE_2 2
#define TYPE_3 4
#define TYPE_4 8
To set one of the types, you would use either:
mytypes = mytypes | TYPE_1;
or, as a shorthand in C,
mytypes |= TYPE_1;
in either case, any existing bits (types) present in mytypes are preserved.
To check whether mytypes contains a specific type, you would write:
if ((mytypes & TYPE_1) == TYPE_1)
Similarly, if you wanted to check for two types at once, you would write:
if (((mytypes & (TYPE_1 | TYPE_3)) == (TYPE_1 | TYPE_3))
where TYPE_1 | TYPE_3 equals 5 as per your example.