# Thread: Bitwise AND return value?

1. ## 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. 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. 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.

4. Originally Posted by 127.0.0.1
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. Great, thanks Tater and angel for the help