What does |= (single pipe equal) and &=(single ampersand equal) mean
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
What does the |= Operator mean?
You know how x += 1
means x = x + 1
, well x |= 1
means x = x | 1
. Of course |
means bitwise OR.
What does |= operator do?
dirtyFlag |= 0x0001
is equivalent to dirtyFlag = dirtyFlag | 0x0001
. The |
operator is the bitwise OR operator. In your case, it sets the lowest binary bit. Some further examples:
1 | 2 = 3 (0001 | 0010 = 0011)
2 | 4 = 6 (0010 | 0100 = 0110)
5 | 1 = 5 (0101 | 0001 = 0101)
What does & (single ampersand), and | (single pipe) operator do in comparison operation?
Those are both Bitwise Operators, which let you manipulate the actual data bits.
The information below was copied as-is from the official documentation:
Bitwise AND Operator
The bitwise AND operator (&
) combines the bits of two numbers. It returns a new number whose bits are set to 1
only if the bits were equal to 1
in both input numbers:
Bitwise OR Operator
The bitwise OR operator (|
) compares the bits of two numbers. The operator returns a new number whose bits are set to 1
if the bits are equal to 1
in either input number:
What is the difference between the | and || or operators?
Just like the &
and &&
operator, the double Operator is a "short-circuit" operator.
For example:
if(condition1 || condition2 || condition3)
If condition1 is true, condition 2 and 3 will NOT be checked.
if(condition1 | condition2 | condition3)
This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.
There is one big caveat, NullReferences or similar problems. For example:
if(class != null && class.someVar < 20)
If class is null, the if-statement will stop after class != null
is false. If you only use &, it will try to check class.someVar
and you get a nice NullReferenceException
. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.
No one ever uses the single &
or |
operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The &
operator does "run these 3 functions, and if one of them returns false, execute the else block", while the |
does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.
There is a Second use of the |
and &
operator though: Bitwise Operations.
What does |= mean? (pipe equal operator)
|=
reads the same way as +=
.
notification.defaults |= Notification.DEFAULT_SOUND;
is the same as
notification.defaults = notification.defaults | Notification.DEFAULT_SOUND;
where |
is the bit-wise OR operator.
All operators are referenced here.
A bit-wise operator is used because, as is frequent, those constants enable an int to carry flags.
If you look at those constants, you'll see that they're in powers of two :
public static final int DEFAULT_SOUND = 1;
public static final int DEFAULT_VIBRATE = 2; // is the same than 1<<1 or 10 in binary
public static final int DEFAULT_LIGHTS = 4; // is the same than 1<<2 or 100 in binary
So you can use bit-wise OR to add flags
int myFlags = DEFAULT_SOUND | DEFAULT_VIBRATE; // same as 001 | 010, producing 011
so
myFlags |= DEFAULT_LIGHTS;
simply means we add a flag.
And symmetrically, we test a flag is set using &
:
boolean hasVibrate = (DEFAULT_VIBRATE & myFlags) != 0;
Understanding the behavior of a single ampersand operator (&) on integers
Compare the binary representations of each of those.
110 & 010 = 010
1010 & 0101 = 0000
10100 & 11001 = 10000
1111011 & 0010100 = 0010000
In each case, a digit is 1 in the result only when it is 1 on both the left AND right side of the input.
Related Topics
Inject Service into Action Filter
How Much Memory Does a C#/.Net Object Use
Programmatically Set Browser Proxy Settings in C#
Determine What Control the Contextmenustrip Was Used On
Where's the Datetime 'Z' Format Specifier
Why Is Array.Length an Int, and Not an Uint
How to Split a String by Strings and Include the Delimiters Using .Net
How to Protect Resources That May Be Used in a Multi-Threaded or Async Environment
Differencebetween Getter-Only Auto Properties and Expression Body Properties
How to Obfuscate My C# Code, So It Can't Be Deobfuscated So Easily
How to Find All Implementations of an Interface
Why Does My C# Gzip Produce a Larger File Than Fiddler or PHP
Move Window When External Application's Window Moves
What's the Best Way of Accessing Field in the Enclosing Class from the Nested Class