157

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

0

3 Answers 3

214

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
4
  • 2
    x = x | (y); is a better way to describe it because x |= y + z; is not the same as x = x | y + z;
    – IronMensan
    Aug 4, 2011 at 13:47
  • thanks for answers / but for my purpose(removing system attribute) which one should i use (|= or &=)? Aug 4, 2011 at 13:48
  • 1
    @LostLord: Folder.Attributes &= ~FileAttributes.System; Aug 4, 2011 at 13:49
  • 1
    Side note: |= and &= can be used as logical operators as well as bitwise operators. Jan 6 at 19:46
44

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

6
  • 2
    What does it mean that a is only evaluated once? Why would it be evaluated more times than that?
    – silkfire
    Oct 20, 2018 at 21:43
  • 1
    @silkfire This is called short-circuit evaluation, see en.wikipedia.org/wiki/Short-circuit_evaluation
    – Polluks
    Jan 16, 2019 at 13:25
  • 1
    @Polluks So basically a |= b actually means a = a || b?
    – silkfire
    Jan 16, 2019 at 14:27
  • @silkfire Yep but don't interchange one pipe and two pipes.
    – Polluks
    Jan 16, 2019 at 14:51
  • 2
    @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 ). Nov 30, 2020 at 10:57
3

I want to remove system attribute with keeping the others..

You can do this like so:

Folder.Attributes ^= FileAttributes.System;
5
  • 1
    I think you want to use a XOR instead of an AND for this.
    – GameZelda
    Aug 4, 2011 at 13:59
  • a bit confused / ~is necessary or not Aug 4, 2011 at 14:01
  • @LostLord The two methods are analogous as far as I'm aware
    – Chris S
    Aug 4, 2011 at 14:22
  • 1
    @ChrisS ^= bit will set the bit if it was not already set, &= ~bit does not set it.
    – Chronicle
    Mar 17, 2019 at 11:15
  • 2
    you definitely don't want to use an xor. That would put it back if it was gone.
    – John Lord
    Jan 2, 2020 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.