Thread: Explanation pls

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Explanation pls

    I came across a snippet
    Code:
    #define OPTION1 "option1" 
    #define OPTION1_BITPOS (0x00000001) 
    #define OPTION2 "option2" 
    #define OPTION2_BITPOS (0x00000002) 
    #define OPTION3 "option3" 
    #define OPTION3_BITPOS (0x00000004) 
    
    //Required to do the bit operations. 
    #define ALL_BITPOS (0x0001ffff)
    what does 0x000000.... do in general

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I'm not sure I understand your question. It's just hexadecimal notation. They could have been defined with the decimal constants 1, 2, and 4 - the result would be the same. Or are you asking something else entirely?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    i came across this to set the appropriate option bits bits dpeending on the input....
    but some one explain me about hexamdecimal values dealt here ... and why they are dealt here.
    Code:
    A simple C command line utility takes a series of command line options. The options are given to the utility like this : <utility_name> options=[no]option1,[no]options2,[no]option3?... Write C code using bitwise operators to use these flags in the code. 
    
    
    
    //Each option will have a bit reserved in the global_options_bits integer. The global_options_bits 
    // integer will have a bit set or not set depending on how the option was specified by the user. 
    // For example, if the user said nooption1, the bit for OPTION1 in global_options_bits 
    // will be 0. Likewise, if the user specified option3, the bit for OPTION3 in global_options_bits 
    // will be set to 1. 
    
    #define OPTION1 "option1" // For strcmp() with the passed arguments. 
    #define OPTION1_BITPOS (0x00000001) // Bit reserved for this option. 
    #define OPTION2 "option2" 
    #define OPTION2_BITPOS (0x00000002) 
    #define OPTION3 "option3" 
    #define OPTION3_BITPOS (0x00000004) 
    
    //Required to do the bit operations. 
    #define ALL_BITPOS (0x0001ffff) 
    
    // Assume you have already parsed the command line option and that 
    // parsed_argument_without_no has option1 or option2 or option3 (depending on what has 
    // been provided at the command line) and the variable negate_argument says if the 
    // option was negated or not (i.e, if it was option1 or nooption1) 
    
    if (strcmp((char *) parsed_argument_without_no, (char *) OPTION1) == 0) 
    { 
        // Copy the global_options_bits to a temporary variable. 
        tmp_action= global_options_bits; 
    
        if (negate_argument) 
        { 
            // Setting the bit for this particular option to 0 as the option has 
            // been negated. 
            action_mask= ~(OPTION1_BITPOS) & ALL_BITPOS; 
            tmp_action= tmp_action & action_mask; 
        }  
        else 
        { 
            //Setting the bit for this particular option to 1. 
            action_mask= (OPTION1_BITPOS); 
            tmp_action= tmp_action | action_mask; 
        } 
    
        // Copy back the final bits to the global_options_bits variable 
        global_options_bits= tmp_action; 
    } 
    else if (strcmp((char *) parsed_argument_without_no, (char *) OPTION2) == 0) 
    { 
        //Similar code for OPTION2 
    } 
    else if (strcmp((char *) parsed_argument_without_no, (char *) OPTION3) == 0) 
    { 
        //Similar code for OPTION3 
    } 
    
    
    //Now someone who wishes to check if a particular option was set or not can use the 
    // following type of code anywhere else in the code. 
    if(((global_options_bits & OPTION1_BITPOS) == OPTION1_BITPOS) 
    {  
        //Do processing for the case where OPTION1 was active.  
    } 
    else 
    { 
        //Do processing for the case where OPTION1 was NOT active. 
    }

  4. #4
    Trying to Learn C nathanpc's Avatar
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    72
    Those 0x000000.... are machine instructions.
    Follow Me At Twitter
    Eee PC 904HD White | Windows XP Home Edition and Linux Ubuntu Hardy Herron

    Google Talk: [email protected]
    ICQ: 424738586
    AIM: nathanjava

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by nathanpc View Post
    Those 0x000000.... are machine instructions.
    it just integer numbers in hex notation... of course we could write the machine instructions in this notation as well as any over data stored in the computer...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's just easier to tell what bits are set in 0x1ffff (i.e., the last 17 of them) then it would be with the decimal number 262143.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Sometimes those leading zeros are necessary. For example:
    0xDCBA might otherwise get interpreted as 0xFFFFDCBA when you meant 0x0000DCBA. Of course you can also write 0xDCBAUL
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. @ explanation pls
    By WDT in forum C# Programming
    Replies: 4
    Last Post: 04-22-2009, 08:23 PM
  2. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  3. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  4. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM
  5. pls help me!!
    By hanseler in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:46 PM