DEV Community

Discussion on: Bitmasks: A very esoteric (and impractical) way of managing booleans

Collapse
 
guneyozsan profile image
Guney Ozsan • Edited

Definitely not an expert but my take away from my limited experience with bit masks are like this:

  1. Bitmasks can help abstraction by not exposing the object(s) being set.
  2. They are helpful for setting on/off switches for an undeterminate number of sources with undeterminate names.
  3. They also help toggling or setting a large or undeterminate number of switches with a single operation.
  4. They are helpful to store various preset configuration values for a large or undeterminate number of switches in single variables.

For example you can have presets like admin = 24; user = 17; poweruser = 31;. And just set config = admin and you are good to go rather than assigning many variables.

Another example, bit masks are used in Unity 3D engine to set the layers a Camera renders in runtime. Unity has two parts, an editor where you set things using GUI, and your code. Layer count and names are customized in the Unity editor. And in your camera code, instead of using a whole list of exposed layer objects, you can provide a bit mask to modify which layers are rendered with that particular camera.

Collapse
 
somedood profile image
Basti Ortiz

Yeah, I also have very limited experience with bitmasks myself. Seems like bitmasking isn't as impractical as I initially thought. Thanks for sharing!