Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does |= (single pipe equal) and &=(single ampersand equal) mean

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...

like image 482
SilverLight Avatar asked Aug 04 '11 13:08

SilverLight


People also ask

What does |= mean in coding?

The bitwise OR assignment operator ( |= ) uses the binary representation of both operands, does a bitwise OR operation on them and assigns the result to the variable.

What does |= mean in C#?

In this article operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null. Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null .

Is and || the same?

In short, the difference between the two operators boils down to the difference between falsy and null/undefined . Where the logical or ( || ) operator takes the right operand in the case of a falsy value — which includes empty string, 0, false, NaN, etc.

What does single pipe mean in C#?

A single pipe (|) is the bitwise OR operator. Two pipes (||) is the logical OR operator.


2 Answers

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 except System" (~ 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
like image 197
Jon Skeet Avatar answered Sep 29 '22 08:09

Jon Skeet


  • | 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

like image 24
Armen Tsirunyan Avatar answered Sep 29 '22 08:09

Armen Tsirunyan