Dear all,
i have found some line someone's program that use & symbol in condition statement and used as operator while assigning value to variable.
example code:
>>>>if (p.revents & POLLIN) <<< a single & symbol in condition statement
>>>> myVar & = var ; <<< when assigning in to variable
My question :
What single & symbol is used for like sample code above,? I guess it should be different like & symbol use AND in condition statement, and for address memory of variable .
Sorry, for my english, but i hope you may understand my question .
Thanks
&& is the boolean "AND" operator.
& has two meanings:
1) it can be used to take the address of a variable;
2) it is the bitwise "AND" operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// This is a bitwise AND. p.revents is bitwise ANDed with POLLIN. The result is
// another bit pattern, of course. But if() requires a boolean expression inside it,
// so the bit pattern is then implicitly converted to a boolean value using the rule
// that zero is false and non-zero is true.
if( p.revents & POLLIN ) { /* ... */ }
// This is a combination bitwise AND and assignment.
myVar &= var;
// The above is equivalent to
myVar = myVar & var;
// and is just a shorter way to write it.
// In this example '&' is taking the address of x.
int x;
int* pint = &x;
// The compiler can deduce which meaning to give to '&' by the context in
// which it is used. Notice that & used as a bitwise AND operator requires two
// "parameters", ie, firstParam & secondParam. Notice that & used as the
// address-of operator requires only one "parameter", ie, &firstParam.