1. ## Bit-flags questions

I have an enumeration called TokenType consist of a series of token type bit flags.

Code:
```enum TokenType {
TOKEN_TYPE_NUMBER                       = 0x10,
TOKEN_TYPE_NUMBER_DECIMAL               = TOKEN_TYPE_NUMBER | 1,
TOKEN_TYPE_NUMBER_POINT                 = TOKEN_TYPE_NUMBER | 3,

TOKEN_TYPE_OPERATOR                     = 0x20,
TOKEN_TYPE_OPERATOR_ASSIGNMENT          = TOKEN_TYPE_OPERATOR | 1,
.
.
TOKEN_TYPE_OPERATOR_SUBSTRACTION        = TOKEN_TYPE_OPERATOR | 3,
TOKEN_TYPE_OPERATOR_MULTIPLICATION      = TOKEN_TYPE_OPERATOR | 4,
TOKEN_TYPE_OPERATOR_DIVISION            = TOKEN_TYPE_OPERATOR | 5,
TOKEN_TYPE_OPERATOR_MODULATION          = TOKEN_TYPE_OPERATOR | 6,

TOKEN_TYPE_PARENTHESIS                  = 0x40,
TOKEN_TYPE_PARENTHESIS_OPEN             = TOKEN_TYPE_PARENTHESIS | 1
TOKEN_TYPE_PARENTHESIS_CLOSE            = TOKEN_TYPE_PARENTHESIS | 2
}

#define TOKEN_TYPE enum TokenType```

It is both operator and operator assignment.

Code:
```Token type
`-- Operator
`-- Assignment Operator
`-- Substraction Operator
`-- ...```
So I can do something like:

Code:
```if(tokenType && TOKEN_TYPE_OPERATOR) {
} else if(tokenType & TOKEN_TYPE_OPERATOR_SUBSTRACTION) {
//parse substraction instruction
} else if(tokenType & TOKEN_TYPE_OPERATOR_ASSIGNMENT) {
} else if(...) {
//...
}
} else if(...) {
//...
}
}```
Is there a formula or something to solve problems like this?

Sorry, I'm not good at math and don't understand bits thing well.

2. Depends on what you are actually trying to achieve.

If I understand it right, you are trying to write a parser. In which case, I'd suggest you just use a straight forward enum with all the variants of tokens that you need to support - there is no real good reason to group them or involve bit patterns.

Code:
`#define TOKEN_TYPE enum TokenType`
What's wrong with:
Code:
`typedef enum TokenType TOKEN_TYPE;`
?

--
Mats

3. Code:
`#undef TOKEN_TYPE`

Thanks matsp.

EDIT:
there is no real good reason to group them or involve bit patterns.
Sorry, I forget to mention this one.

There is certain condition to use bit pattern especialy in my recursive descent algorithm.

For instance,
Code:
```while(global.token.type & TOKEN_TYPE_OPERATOR) {
operator();
operand();
}```
Code:
```void operator(void) {
if(global.token.type & TOKEN_TYPE_OPERATOR_BITWISE_AND) {
//parse operator '&'
puts('POP temp2');
puts('AND temp1, temp2');
.
.
} else if(...) {
.
.
} else if(global.token.type & TOKEN_TYPE_OPERATOR_X) {
//parse operator x
}
}```