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

111,642

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

Solution 2

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;
Share:
111,642
SilverLight
Author by

SilverLight

WEB DEVELOPER & C# PROGRAMMER ASP.NET C# JQUERY JAVASCRIPT MICROSOFT AJAX MICROSOFT SQL SERVER VISUAL STUDIO

Updated on July 08, 2022

Comments

  • SilverLight
    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
    IronMensan almost 13 years
    x = x | (y); is a better way to describe it because x |= y + z; is not the same as x = x | y + z;
  • SilverLight
    SilverLight almost 13 years
    thanks for answers / but for my purpose(removing system attribute) which one should i use (|= or &=)?
  • George Duckett
    George Duckett almost 13 years
    @LostLord: Folder.Attributes &= ~FileAttributes.System;
  • GameZelda
    GameZelda over 12 years
    I think you want to use a XOR instead of an AND for this.
  • SilverLight
    SilverLight over 12 years
    a bit confused / ~is necessary or not
  • MordechayS
    MordechayS over 12 years
    @LostLord The two methods are analogous as far as I'm aware
  • silkfire
    silkfire over 5 years
    What does it mean that a is only evaluated once? Why would it be evaluated more times than that?
  • Polluks
    Polluks over 5 years
    @silkfire This is called short-circuit evaluation, see en.wikipedia.org/wiki/Short-circuit_evaluation
  • silkfire
    silkfire over 5 years
    @Polluks So basically a |= b actually means a = a || b?
  • Polluks
    Polluks over 5 years
    @silkfire Yep but don't interchange one pipe and two pipes.
  • Chronicle
    Chronicle about 5 years
    @ChrisS ^= bit will set the bit if it was not already set, &= ~bit does not set it.
  • John Lord
    John Lord over 4 years
    you definitely don't want to use an xor. That would put it back if it was gone.
  • O. R. Mapper
    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
    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.