To address your first question: Your problem is operator precedence and understanding of how bitwise operators work.
if (lexStatus & fInCommentBlock == fInCommentBlock)
This works for you only because ==
have higher precedence than &
so fInCommentBlock == fInCommentBlock
is always true, hence the same thing as lexStatus & 1
, which is the same thing as lexStatus & OnlySpaces
. This will be true when lexStatus == initState
because initState
includes flag OnlySpaces
.
Is there a better way?
You want to abstract the bitwise comparison into one or two helper functions.
int LexStatus_Is(int flags) { return (lexStatus & flags) == flags; }
int LexStatus_IsNot(int flags) { return (lexStatus & flags) != flags; }
Then you can write:
if (LexStatus_IsNot(fInComment))
Which would be more intuitive.