What is & = and | =

I looked at VC ++ code in a large code base and came across this:

if (nState & TOOL_TIPS_VISIBLE) nState &= ~TOOL_TIPS_VISIBLE; else nState |= TOOL_TIPS_VISIBLE; break; 

Is there an operator like & = or | = in C ++? For what? Is this the equivalent of nState = nState & ~TOOL_TIPS_VISIBLE ?

+7
source share
7 answers

x &= y same as x = x & y
x |= y coincides with x = x | y x = x | y

+9
source

What was not mentioned is that the operators &= and |= can be overloaded. So the code you posted depends on the type of nState (although it is quite clearly an int, so most likely this is not applicable here). Overloading &= does not imply overloading & , therefore, in this case

 x &= y might not be the same as x = x & y 

This may also depend on what TOOL_TIPS_VISIBLE .

 struct s{ int x; }; void operator &= (int& x, sy) { x = 0; } 

Now that you do:

 s TOOL_TIPS_VISIBLE; x &= TOOL_TIPS_VISIBLE; 

x will become 0 . Again, it is unlikely, but nonetheless useful to know.

All other answers are probably applicable here, but it is worth considering.

+7
source

Its bitwise and

in the first case, the flag (bit) is off

 nState &= ~TOOL_TIPS_VISIBLE 

in the second case, the flag is on

 nState |= TOOL_TIPS_VISIBLE 
+3
source

x &= y means x = x & y . So yes, you are right.

+2
source

Yes. &= matches & , which += equals +

+2
source
 x &= y; 

equivalent to:

 x = x & y; 

In the same way

 x|= y; 

equivalent to:

 x = x | y; 
+1
source

& and | similar to && and || , only they work out dimensionally. So now you can imagine that &= and |= work similarly to += . This is x &= y; ==> x = x & y; x &= y; ==> x = x & y;

+1
source

All Articles