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

Leave a Comment