A bitmask is a representation of many states as a single value.
A bit is a 0 or a 1. In a bitmask 0 is an inactive layer, and a 1 is active.
As int is a 32 bit value, it can be used to represent 32 toggleable layers.
The LayerMask struct describes a bitmask of layers used by physics and rendering APIs in Unity.
It's implicitly converted to int when using physics methods.
Because a LayerMask is a bitmask, it shouldn't be treated as a single layer.
Generally masks are declared as an enum so they can be easily referenced and manipulated by name.
The Flags attribute indicates the enum is a bitmask, and modifies ToString to return more relevant values.
Enums are int values by default, but any integral numeric type can be used when defining an enum.
We could use an underlying type of byte, as the range is known to be less than 8 layers. Optimisations like these may decrease performance due to memory alignment.
Create an inverted mask using the ~ operator, where the layer to remove is now a 0, the rest 1's.
Perform a logical and. If a bit is enabled (1) in one mask and the other, it will be in our result.
Because the layer is a 0, it will not be in both masks, and is removed.
Note that this value may not be the same as "Everything" or "All", and different implementations may or may not interpret it that way. Note how Days.Everyday is 01111111, not all bits are enabled. Your implementations may need to consider this.
If setOn is true then ~mask will invert the original mask, & Days.Weekend isolates the bits we want to enable, giving us a mask that only contains the weekend bits that aren't enabled in the original mask. We then toggle the mask with this value.
If setOn is false then mask does nothing, & Days.Weekend isolates the bits we want to disable, giving us a mask which contains the weekend bits that are enabled in the original mask. We then toggle the mask with this value.