Enum
De drev
BitWise Operations
Your enumeration needs to be powers of two :
enum {
TAKES_DAMAGE = 1, GRABBABLE = 2, LIQUID = 4, SOME_OTHER = 8
};
Or in a more readable fashion :
enum {
TAKES_DAMAGE = 1 << 0, GRABBABLE = 1 << 1, LIQUID = 1 << 2, SOME_OTHER = 1 << 3
};
Why ? Because you want to be able to combine flags with no overlapping, and also be able to do this:
if(myVar & GRABBABLE) {
// grabbable code
}
... Which works if the enumeration values look like this :
TAKES_DAMAGE: 00000001 GRABBABLE: 00000010 LIQUID: 00000100 SOME_OTHER: 00001000
So, say you've set myVar to GRABBABLE | TAKES_DAMAGE, here's how it works when you need to check for the GRABBABLE flag:
myVar: 00000011 GRABBABLE: 00000010 [AND] ------------------- 00000010 // non-zero => converts to true
If you'd set myVar to LIQUID | SOME_OTHER, the operation would have resulted in :
myVar: 00001100 GRABBABLE: 00000010 [AND] ------------------- 00000000 // zero => converts to false
Match any
enum {
TAKES_DAMAGE = 1 << 0, GRABBABLE = 1 << 1, LIQUID = 1 << 2, SOME_OTHER = 1 << 3, ANY = 0b1111
};