Boolean Compound Assignment Operators
I ran across some code the other day I don't really recall ever seeing:
x |= y
I initially confused this with the Ruby memoization operator (||=
). Then I realized this is just a regular compound assignment operator, most commonly seen with arithmetic operations like x += y
. As you would expect, you can use both &
and |
in this way.
This is useful for boolean operations, which occur fairly frequently in some of the Xamarin (mobile) work I've been doing.
Folder.Attributes |= FileAttributes.Directory | FileAttributes.Hidden
I bonus thing I leared when looking into this, is the use of the bitwise-NOT: ~FileAttributes.System
. This allows you to say "all values except System
.