I was going through some VC++ code in a large code-base and came across this:
if (nState & TOOL_TIPS_VISIBLE)
nState &= ~TOOL_TIPS_VISI
&
and |
are similar to &&
and ||
, only they works in bitwise fashion. So now you could imagine &=
and |=
works similar to +=
. That is x &= y; ==> x = x & y;
What hasn't been mentioned is that both &=
and |=
operators can be overloaded. Therefore, the code you posted depends on the type of nState (although it's quite clearly an int, so most probably this doesn't apply here). Overloading &=
does not implicitly overload &
, so in this case
x &= y might not be the same as x = x & y
It might also depend on what TOOL_TIPS_VISIBLE
is.
struct s{
int x;
};
void operator &= (int& x, s y)
{
x = 0;
}
Now whenever you do:
s TOOL_TIPS_VISIBLE;
x &= TOOL_TIPS_VISIBLE;
x
will become 0
. Again, highly unlikely, but good to know nevertheless.
All other answer probably apply here, but this is worth considering.
Its bitwise and or
in the first case the flag (bit) is turned off
nState &= ~TOOL_TIPS_VISIBLE
in the second case the flag is turned on
nState |= TOOL_TIPS_VISIBLE
x &= y
is the same as x = x & y
x |= y
is the same as x = x | y
Yes. &=
is to &
what +=
is to +
x &= y
means x = x & y
. So yes you're right.