Thread: Bit-flags questions

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Talking 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_HEXADECIMAL           = TOKEN_TYPE_NUMBER | 2,
      TOKEN_TYPE_NUMBER_POINT                 = TOKEN_TYPE_NUMBER | 3,
    
      TOKEN_TYPE_OPERATOR                     = 0x20,
      TOKEN_TYPE_OPERATOR_ASSIGNMENT          = TOKEN_TYPE_OPERATOR | 1,
      TOKEN_TYPE_OPERATOR_ASSIGNMENT_ADDITION = ?
      .
      .
      TOKEN_TYPE_OPERATOR_ADDITION            = TOKEN_TYPE_OPERATOR | 2,
      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
    How about TOKEN_TYPE_OPERATOR_ASSIGNMENT_ADDITION value?

    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) {
      if(tokenType & TOKEN_TYPE_OPERATOR_ADDITION) {
        //parse addition instruction
      } else if(tokenType & TOKEN_TYPE_OPERATOR_SUBSTRACTION) {
        //parse substraction instruction
      } else if(tokenType & TOKEN_TYPE_OPERATOR_ASSIGNMENT) {
        if(tokenType & TOKEN_TYPE_OPERATOR_ASSIGNMENT_ADDITION) {
          //parse addition assignment instruction
        } 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.

    Thanks in advance.
    Just GET it OFF out my mind!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    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
      }
    }
    Please, anyone? xD
    Last edited by audinue; 12-23-2008 at 11:50 AM.
    Just GET it OFF out my mind!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. Suggestions on a bit of FILE I/O anyone??
    By blondie.365 in forum C Programming
    Replies: 15
    Last Post: 10-20-2008, 02:03 PM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Bit Flags
    By Padawan in forum C Programming
    Replies: 15
    Last Post: 03-30-2004, 10:38 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM