What does |= (single pipe equal) and &=(single ampersand equal) mean
Solution 1
They're compound assignment operators, translating (very loosely)
x |= y;
into
x = x | y;
and the same for &
. There's a bit more detail in a few cases regarding an implicit cast, and the target variable is only evaluated once, but that's basically the gist of it.
In terms of the non-compound operators, &
is a bitwise "AND" and |
is a bitwise "OR".
EDIT: In this case you want Folder.Attributes &= ~FileAttributes.System
. To understand why:
-
~FileAttributes.System
means "all attributes exceptSystem
" (~
is a bitwise-NOT) -
&
means "the result is all the attributes which occur on both sides of the operand"
So it's basically acting as a mask - only retain those attributes which appear in ("everything except System"). In general:
-
|=
will only ever add bits to the target -
&=
will only ever remove bits from the target
Solution 2
-
|
is bitwise or -
&
is bitwise and
a |= b
is equivalent to a = a | b
except that a
is evaluated only once
a &= b
is equivalent to a = a & b
except that a
is evaluated only once
In order to remove the System bit without changing other bits, use
Folder.Attributes &= ~FileAttributes.System;
~
is bitwise negation. You will thus set all bits to 1 except the System bit. and
-ing it with the mask will set System to 0 and leave all other bits intact because 0 & x = 0
and 1 & x = x
for any x
Solution 3
I want to remove system attribute with keeping the others..
You can do this like so:
Folder.Attributes ^= FileAttributes.System;
SilverLight
WEB DEVELOPER & C# PROGRAMMER ASP.NET C# JQUERY JAVASCRIPT MICROSOFT AJAX MICROSOFT SQL SERVER VISUAL STUDIO
Updated on July 08, 2022Comments
-
SilverLight almost 2 years
In below lines:
//Folder.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly; Folder.Attributes |= FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly; Folder.Attributes |= ~FileAttributes.System; Folder.Attributes &= ~FileAttributes.System;
What does
|=
(single pipe equal) and&=
(single ampersand equal) mean in C#?I want to remove system attribute with keeping the others...
-
IronMensan almost 13 years
x = x | (y);
is a better way to describe it becausex |= y + z;
is not the same asx = x | y + z;
-
SilverLight almost 13 yearsthanks for answers / but for my purpose(removing system attribute) which one should i use (|= or &=)?
-
George Duckett almost 13 years@LostLord:
Folder.Attributes &= ~FileAttributes.System;
-
GameZelda over 12 yearsI think you want to use a XOR instead of an AND for this.
-
SilverLight over 12 yearsa bit confused / ~is necessary or not
-
MordechayS over 12 years@LostLord The two methods are analogous as far as I'm aware
-
silkfire over 5 yearsWhat does it mean that
a
is only evaluated once? Why would it be evaluated more times than that? -
Polluks over 5 years@silkfire This is called short-circuit evaluation, see en.wikipedia.org/wiki/Short-circuit_evaluation
-
silkfire over 5 years@Polluks So basically
a |= b
actually meansa = a || b
? -
Polluks over 5 years@silkfire Yep but don't interchange one pipe and two pipes.
-
Chronicle about 5 years@ChrisS
^= bit
will set the bit if it was not already set,&= ~bit
does not set it. -
John Lord over 4 yearsyou definitely don't want to use an xor. That would put it back if it was gone.
-
O. R. Mapper over 3 years@Polluks: I fail to understand your comment about one and two pipes - I think using two pipes instead of one was silkfire's entire point in that last comment. Also, I am not convinced the statement "except that
a
is evaluated only once` does indeed refer to short-circuit evaluation, as short-circuit evaluation does not change the evaluation of the first operand (a
), but might skip the evaluation of the second operand (b
). -
Evgeni Nabokov about 3 years@silkfire it means a "not lazy" ||, i.e. both parts of the expression are always calculated, whereas 2nd part of a standard "lazy" || may not be calculated.