Thread: Bitwise AND return value?

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92

    Bitwise AND return value?

    Sorry if this question is simple, but I was unable to verify what this line of code does through google searching.

    I am confused as to how the if statement with the bitwise AND is evaluated.

    Code:
    #define TT_VALUESVALID 0x10000 // set if intvalue and floatvalue are valid
    ...
    int subtype; // token sub type
    ...
    if ( !(subtype & TT_VALUESVALID) ) {
        NumberValue();
    }
    ..

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    The result of the AND operation will be an int, as normal. In C an int is "true" if it is non-zero and "false" if it is 0. So the (subtype & TT_VALUESVALID) will be evaluated, then the ! will invert the true/falseness. E.g. if the result of "subtype & TT_VALUESVALID" was 0x10000, 0x10000 is non zero so 'true', so !0x10000 is 0, 'false'.

    Written as a sentence the if statement would be "if there are no set bits in common in subtype and TT_VALUESVALID then execute NumberValue()."

    Dunno if that helps at all, not really sure what you're asking! Which bit are you confused about?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It tests if the 17th least significant bit of subtype is set. 17th, because 0x10000 is 2^16, and one more for the ones place.
    Last edited by King Mir; 11-26-2011 at 07:22 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 127.0.0.1 View Post
    Sorry if this question is simple, but I was unable to verify what this line of code does through google searching.

    I am confused as to how the if statement with the bitwise AND is evaluated.

    Code:
    #define TT_VALUESVALID 0x10000 // set if intvalue and floatvalue are valid
    ...
    int subtype; // token sub type
    ...
    if ( !(subtype & TT_VALUESVALID) ) {
        NumberValue();
    }
    ..
    Ok... sit down an analyse it step by step...

    ! means NOT ... so if the bracketed function returns true (non-0) it is flipped over to false (0)

    The expression itself will AND your subtype variable with 0x10000 .. that is a bitwise operation in which only matching bits (1 in both values) are returned... so if you AND subtype with 0x10000, which is a single bit, you will either get 0x10000 or 0 back...

    If the expression evaluates to 0 it would read as false but NOT flips it over to true... and the numbervalue() function is executed.
    If the expression evaluates to 0x10000 it would read as true, NOT flips it to false... and the numbervalue() function is not executed.

    Do some reading up on if() statements and bitwise operations to get more detail.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Look in your attic, I am there...
    Posts
    92
    Great, thanks Tater and angel for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise? am i right?
    By skiz in forum C Programming
    Replies: 10
    Last Post: 03-11-2009, 11:37 PM
  2. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  3. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Replies: 4
    Last Post: 07-15-2005, 04:10 PM